Welcome

C Programming Tutorial


C Bitwise Operators

Bitwise Operators Operates on Integral Expressions

  • &: Bitwise `AND` Operator
  • |: Bitwise `OR` Operator
  • ^: Bitwise `Exclusive Or` Operator
  • ~: Bitwise `NOT` Operator
  • >>: Bitwise `Right Shift` Operator
  • <<: Bitwise `Left Shift` Operator

Bitwise `AND` Operator (&)

Op1 & op2 both bit's are 1s then result will be 1 otherwise 0

		2 & 3  ==> 2
decimal 2 binary reprasentation		010
decimal 3 binary reprasentation		011
					------
result is 2				010
					------
		//Here second bit from both operands are 1s ,result is 1 otherwise 0.
		

Bitwise `OR` Operator (|)

Op1 | op2 either bit or both bits are 1 then result will be 1 otherwise 0

		2 | 3  ==> 3
decimal 2 binary reprasentation		010
decimal 3 binary reprasentation		011
					------
result is 3				011
					------
		//Here first bit from let to right op1 has 0 and operand 2 is 1 so result is 1.
		//Sencond bit of operand1 is 1 and second bit of operand is 1 is result is 1.
		

Bitwise `Exclusive OR` Operator (^)

Op1 ^ op2 either bit is 1 then result will be 1 otherwise 0

		2 ^ 3  ==> 3
decimal 2 binary reprasentation		010
decimal 3 binary reprasentation		011
					------
result is 1				001
					------
		//Here first bit from let to right op1 has 0 and operand 2 is 1 so result is 1.
		//Sencond bit of operand1 is 1 and second bit of operand is 1 is result is 0.
		//Third bit of operand1 is 0 and second bit of operand is 0 is result is 0.
		

Bitwise `NOT ` Operator (~)

~ Op1 it flips 0s into 1s and 1s into 0s

		 ~2  ==> 3
decimal 2 binary reprasentation		0000 010
decimal 3 binary reprasentation		1111 101
					------
result is 1				001
					------
		//Here first bit from let to right op1 has 0 and operand 2 is 1 so result is 1.
		//Sencond bit of operand1 is 1 and second bit of operand is 1 is result is 0.
		//Third bit of operand1 is 0 and second bit of operand is 0 is result is 0.
		

Bitwise `Right Shift` Operator (>>)

Bitwise Right Shift operator right shifts bits by n position
syntax:

			num>>pos;   //Given a number right shift by n positions. 
		
Example:
			// for ex: 4  represented in binary as 0100 
			// 4>>1 ;   4th bit becomes 3rd bit, 3rd bit becomes 2nd bit,2 bit becomes 1st bit,1st bit value removed.
			// 4 right shift by 1 position gives  0010
			//   4>>1 <==>  2
		
**Note: it's also called as a division operator using bit-wise operators

Bitwise `Left Shift` Operator (<<)

Bitwise Left Shift operator left shifts bits by n position
syntax:

			num<<n;   //Given a number left shift by n positions. 
		
Example:
			// for ex: 4  represented in binary as 0100 
			// 4<<1 ;   LSB bit becomes 1st bit, 1st bit becomes 2nd , 3rd bit becomes 4th, 4th bit becomes 5th so on...
			// 4 left shift by 1 position gives  1000
			//   4<<1 <==>  8
		
**Note: it's also called as a multiplication operator using bit-wise operators

Bitwise Operators Examples:

Expression:

a=1;b=2;
Expression:a<<b<<1; <==> (a<<b)>>1;

bitwise operators precedence is from left to right, (a<<b) evaluated first

In this example a is 1 and b is 2 , a left shifted by 2 positions.
i.e: 0001 becomes 0100
1st shift it becomes 2, second shift it becomes 4.
second expression right shift by 1 position
4 is right shifted by 1 position
0100 >>1 <==> 0010; answer is 2.

Find a given number is even or odd using Bitwise Operators

        even numbers have LSB(Least Significant) bit is always set to 0, for odd Numbers LSB bit is always set to 1, so based on this concept, programmer can check like this, use Bitwise operator & with 1 result is 0 means even numbers, 1 means odd number.

		int x=10;
		printf("%d is %s", ((x&1)==0)?"even":"odd";  //prints 10 is even
		x=11;
		printf("%d is %s", ((x&1)==0)?"even":"odd";  //prints 11 is odd

	

Find set bits in a Number using Bitwise Operators

        counting number of 1s in any Number, for example decimal 2 in binary 0010 ,it has 1 set bit, for decimal 6 in binary 0110 has set bits 2 etc.,

		int N=10;
		int c = setBits(N);
		printf("\nNumber of set Bits for N=%d is %d\n",N,c);

		int setBits(int N)
		{
			int c=0;
			
			while(N!=0){
				
				if(N&1==1)c++;
				N=N>>1;
			}
		return c;
		}
	
output
		Number of set Bits for N=10 is 2
	

Swap 2 Numbers using bitwise Operators

 Using Exclusive OR(^),we can swap 2 numbers. first statement a= a^b; 10 ^20 gives 30,now a has 30
b=a^b 30 ^20 gives 10, now b has 10, a=a^b 30^10 gives 20 that is a value.Numbers are exchanged without using third variable

	int a=10,b=20;

        printf("original values a=%d,b=%d\n",a,b);
        a=a^b;
        b=a^b;
        a=a^b;
        printf("after swap a=%d,b=%d\n",a,b);

	
output:
original values a=10,b=20
after swap a=20,b=10

	
8=1000 7=0111 ---------- 00000 10=1010 9 =1001 -------- 1000

ADS