bitwise+Operator
bitwise+Operator
• The Bitwise operator are AND, OR, XOR, Complement, Left shift , Right Shift.
• These operators work on integral type of data and they perform operation on Binary
representation of data I.e, 0’s and 1’s.
• Bitwise operators are used in applications of networking , Encryption and Decryption etc.
• Bitwise calculations starts from right hand side.
• To understand bitwise we rst need to understand binary number system . Binary number
system uses only 0’s and 1’s
• Decimal number system uses numbers from 0 - 9
• To convert decimal to binary we can do it in either 2 ways
Bitwise Operations
Ex : a - 1010
b- 1101
OR operations ( | )
• OR works on addition
• Working of OR - 1 + 1 = 1
1+0=1
0+1=1
0+0=1
Ex : a - 1010
b - 1101
XOR operations ( ^ )
• Working of XOR - 1 ^ 1 = 0
1^0=1
1^1=1
1^0=0
Ex: a - 1010
b - 1101
a^b- 0111 = 7 ( decimal form of binary number)
• a = 10 ( binary of 10 = 1010 )
• If we left shift ‘ a ’ by one place I.e, a << 1 then
1 0 1 0
• After left shift by one place the bit that is freed will be taken as zero.
• Now 10100 = 20 = 2* 10
• If we left shift 20 again then,
1 0 1 0 0
1 0 1 0 0 0 = 40 = 2 * 20
• We see that when we left shift the number will double for one place I.e; a << n , a * 2 ( power n)
• If we double the left shift I.e , a. << 2 then
a << 2 = 2( power n ) * a = 4*10 = 40