In this article, we will discuss the concept of the C code to subtract two integer using Bitwise operator
In this post, we are going to learn how to write a program to find the subtraction of two numbers using Bitwise operator in C programming language
The program allow the user to enter two integers and then calculates subtraction of given numbers using Bitwise operator in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int a,b,num1,num2;//variable declaration printf("Enter the first number: \n"); scanf("%d",&num1); //Ask input from the user and store the input in variable num1; printf("Enter the second number: \n"); scanf("%d",&num2); //Ask input from the user and store the input in variable num2; a=num1; //Assign the value of num1 to a b=num2; //Assign the value of num2 to b while (num2!= 0)//using while loop to calculate subtraction { int bwr=(~num1) & num2; num1=num1^num2; num2=bwr<<1; } printf("Subtraction of %d and %d is %d",a,b,num1); //display result on the screen getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter first number: 100 Enter second number: 55 Subtraction of two numbers 100 and 55 is: 45
Case 2
Enter first number: 55 Enter second number: 100 Subtraction of two numbers 55 and 100 is: -45
The program allow the user to enter two integers and then calculates subtraction of given numbers using Bitwise operator in C language
Program 2
#include <stdio.h> #include <stdlib.h> int sub(int,int); int main() { int num1,num2,ans; printf("Enter the first number "); //ask input from the user scanf("%d",&num1); //store the input number in num1 printf("Enter the second number "); //ask input from the user scanf("%d",&num2); //store the input number in num1 ans=sub(num1,num2);//Calling the function printf("Subtraction of %d and %d is: %d",num1,num2,ans); getch(); return 0; } int sub(int x, int y){//function definition while(y!=0){ int carry=(~x) & y; x=x^y; y=carry<<1; } return x; }
When the above code is executed, it produces the following result
Case 1
Enter first number: 500 Enter second number: 230 Subtraction of two numbers 500 and 230 is: 270
Case 2
Enter first number: 200 Enter second number: 340 Subtraction of two numbers 50 and 23 is: -140
Suggested for you
Function in C language
Similar post
Subtract two numbers in C language
Subtract two numbers using function in C language
Subtract two numbers using recursion in C language
Find sum of two numbers in C language
Find sum of two numbers in C language using pointer
Find sum of two numbers in C language using function
Find sum of two numbers in C using recursion
Find sum of two numbers in C without Arithmetic operator
Find sum of natural numbers in C language
Find sum of natural numbers in C language using recursion
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…