# C Program#158 – Understanding Bitwise Operators in C.

##### Program to Understand Bitwise Operators in C. :

**Brief Info. : Bitwise Operators **

- Bitwise Operators gives the power to directly manipulate bits (binary digits).
- C provides six bitwise operators-

251550

Category Operator Symbol Operator Name Operation Bitwise Logical & Bitwise AND Results 1 if only both operands have 1 otherwise 0. such as 11001000 & 10111000 = 10001000 | Bitwise OR Results 1 if any one of both operands have 1 otherwise 0. such as 11001000 & 10111000 = 11111000 ^ Bitwise XOR (Exclusive OR) Performs XOR means results zero only when we have two zeroes or two ones. such as 11001000 ^ 10111000 = 01110000 Bitwise Shift >> Bitwise Right Shift Shifts Bits to right such as 01110000 >> 1 = 00111000 << Bitwise Left Shift Shifts Bits to Left such as 01110000 << 1 = 11100000 Bitwise 1’s (One’s Complement) ~ Bitwise One’s Complement Flips all bits (1 becomes 0 and 0 becomes 1) ~01110000 = 10001111 - Bitwise AND (&) operator is used (1) to check whether a particular bit in a number is ON or OFF. (2) To turn Off a particular bit in a number.
- Second Operand in Bitwise AND (&) operator is generally called AND MASK, since it masks (covers) the bits of the first operand and gives resultant bit series, which may be used to check particular bits status.
- Bitwise OR (|) operator is used to ON a particular bit in a number.
- Bitwise XOR (^) operator is used to toggle a bit ON or OFF. A number XORed with another number twice gives the orginal number.
- Bitwise XOR (^) operator is used to toggle a bit ON or OFF. A number XORed with another number twice gives the orginal number.
- Bitwise RIGHT SHIFT (>>, Bitwise LEFT SHIFT (<<) & One's Complement (~) operator is used in encryption since they changes the original number.
- Bitwise Operators are used extensively in many areas of Computer Science including Compression, Encryption, Setting Bitfields (flags), Communication Technology (Ports,Checksums,Parity, Stop Bits, Flow Control Algorithms), Embedded Systems etc..
- Still not understood ? See the
**proper working example**in action below.

**Full Well Commented, Self-Explanatory Example Program of the same given below.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/*C Program#158 - Understanding Bitwise Operators in C. Note: Assuming Integer is 32 Bit wide |Source : www.kshitijdivakar.com | Editor : Code::Blocks 16.01 mingW (Windows)*/ #include<stdio.h> int main() { int a,b,c; a=13; //0000 0000 0000 0000 0000 0000 1101 b=25; //0000 0000 0000 0000 0000 0001 1001 c =a & b; //0000 0000 0000 0000 0000 0000 1001 (Decimal = 9) printf("\n\n Result of Bitwise AND (&) : %d",c) ; c =a | b; //0000 0000 0000 0000 0000 0001 1101 = (Decimal = 29) printf("\n\n Result of Bitwise OR (|) : %d",c) ; c =a ^ b; //0000 0000 0000 0000 0000 0000 0001 0100 (Decimal = 20) printf("\n\n Result of Bitwise Exclusive OR (^) : %d",c) ; c=~a; //0000 0000 0000 0000 0000 0000 0000 1101 (Decimal 13) printf("\n\n Result of Bitwise One's Complement (~) in %%d - signed form : %d",c) ; /* 1111 1111 1111 1111 1111 1111 1111 0010 = 1's complement (Signed Decimal = -14) (it is actually negation (applying NOT on 13) it becomes negative as the first bit (left most) indicates sign as - 0 = Positive and 1 = Negative and Negative Numbers are mostly represented in 2's complement by the System 2's complement is 1's complement of a number + 1 means - 1111 1111 1111 1111 1111 1111 1111 0010 + 1 = 11111111111111111111111111110011 (Signed Decimal -13) */ printf("\n\n Result of Bitwise One's Complement (~) in %%u - unsigned form: %u",c) ; //1111 1111 1111 1111 1111 1111 1111 0010 (Unsigned 4,294,967,282) //Highest number represented in unsigned int on 32 bit is 4,294,967,295 //thus 4,294,967,295 - 4,294,967,282 = 13 (unsigned form) c=a<<2; //0000 0000 0000 0000 0000 0000 0000 1101--> 0000 0000 0000 0000 0000 0000 0011 0100 (Decimal = 52) printf("\n\n Result of Bitwise Left Shift (<<) : %d",c) ; c=b>>2; //0000 0000 0000 0000 0000 0000 0001 1001 --> 0000 0000 0000 0000 0000 0000 0000 0110 (Decimal = 6) printf("\n\n Result of Bitwise Right Shift (<<) : %d",c) ; printf("\n\n"); return 0; } |

**Below Here is the Output of the program.**

**Output of the Program :**