Bitwise Operators In C

Definition: A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.
Bitwise operators are used in communication stack.


Operations can be performed on a bit level using bitwise operators. We use bitwise operators to manipulate data  at its lowest bit level.bitwise operators works on each bit of the data.

There are six bitwise operators are in C programming:

Bitwise AND Operator In C, Bitwise OR Operator In C, Bitwise XOR Operator In C, Bitwise operators in C
Above table shows six bitwise operators in C programming:



1. Bitwise AND(&):

It 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.


[It is used to check perticular bit of data is ON(1) or OFF (0).]

Suppose we will take two intiger variables a & b, where a=9 and b=10.

8 bit binary representation of a & b is...

a= 0000 1001
b= 0000 1010

It will be evaluated as- 


Bitwise AND Operator In C, Bitwise Operators In C


So a & b is evaluated as 0000 1000 which is 8 in decimal.




2. Bitwise OR(|):

It 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.


[ It is commonly used to set flag bit values.]

Suppose we will take two intiger variables a and b, where  a=4 and b=11.

8 bit binary representation of a | b is...

a= 0000 0100
b= 0000 1011

It  is evaluated as-

Bitwise OR Operator In C, Bitwise Operators In C

So a | b is evaluated as 0000 1111 which is 15 in decimal.




3. Bitwise XOR(^):

It 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.

Suppose we will take two intiger variables a and b, where a=6 and b=13.

8 bit binary representation of  a^b is....

a= 0000 0110
b= 0000 1101

It is evaluated as-


Bitwise XOR Operator In C, Bitwise operators in C

So a^b is evaluated as 0000 1011 which is 11 in decimal.



4. Bitwise Complement or NOT (~):


It takes one number and inverts all bits of it. Means it  sets bit value 1 only if corresponding bit of operand is 0.


Suppose we take intiger variable a, where a=4.

8 bit binary representation of a is-

a= 0000 0100

It is evaluated as-


Bitwise Complement or Not Operator In C, Bitwise operators in C

So a~ is  evaluated as 1111 1011 which is 124 in decimal.



5. Bitwise left shift(<<):


It takes two numbers and left shift the bits of the first operand, the second operand decides the number of places to shift.


Suppose we take an intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-


Bitwise Left Shift Operator In C, Bitwise operators in C

Here,  ex: a<<1 means it shifts  bit one times to left which is 30  in decimal. another one example is a<<2 means it shifts bit two times to left which is 60 in decimal.
Left shifting of bit causes insertion of zero from right.




6. Bitwise right shift (>>):


It take two numbers and right shift the bits of the first operand, the second operand decides the number of places to shift.

Suppose we take intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-


Bitwise Right Shift Operator In C, Bitwise Operators In C

Here, ex: a>>1 means it shifts bit one time to right which is 7 in decimal. another one example is a>>2 means it shifts bit two time to right which is 3 in decimals.

If you have any queries... feel free to discuss below in the comment box.   
  
Happy Coding....🙃


No comments:

Post a Comment

If you have any doubts, please discuss here...👇