In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C.
- 1.The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- 2.The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
- 3.The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- 4.The << (left shift) in C or C++ takes two numbers, the left shifts the bits of the first operand, and the second operand decides the number of places to shift.
- 5.The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift.
- 6.The __~ (bitwise NOT)__ in C or C++ takes one number and inverts all bits of it.
//Bitwise_operation
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
//To count the number of 1's
int num,count=0;
printf("Enter the number:\n");
scanf("%d",&num);
do{
if(num&1){
count++;
}
num=num>>1;
}while(num!=0);
printf("Number of 1's is:%d",count);
//To check number is even or odd
if(num&0){
printf("Number is odd");
}
else {
printf("Number is even");
}
return 0;
}
//Bitwise_main
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
int x,y,z;
char bitwise;
printf("Enter the Bitwise operator:");
scanf("%c",&bitwise);
printf("Enter the first number:\n");
scanf("%d",&x);
printf("Enter the second number:\n");
scanf("%d",&y);
switch (bitwise)
{
case '&':
z=x&y;
printf("Bitwise AND of x & y is:%d\n",z);
break;
case '|':
z=x|y;
printf("Bitwise OR of x & y is:%d\n",z);
break;
case '^':
z=x^y;
printf("Bitwise XOR of x & y is:%d\n",z);
break;
case '~':
z=x~y;
printf("Bitwise NOT of x & y is:%d\n",z);
break;
default :
printf("Enter a Valid Bitwise Operator(&/|/^/~)");
}
return 0;
}