0

Bitwise Operators

Bitwise operators are used to perform operations on bits. Most of the times programmers works on byte, int, char etc. data types and does not pay attention to bit level operations. However bit operators are used to manipulate values for comparisons and calculation.

Bits always have value either ‘1’ or ‘0’.  Binary operators are used to manipulate these values. In simple words ‘1’ means bit is ‘set’ and ‘0’ means bit is ‘clear’.

Following are few operators used to perform bitwise operations:

  • NOT/Complement :

This is a unary operator which means it works on only one operand. Not operator flips each bit means 1 become 0 and 0 become 1.

Not 5: ~5 :: Not 0101 :: result will be 1010 (10).

  • AND ( & ) :

Binary And operator compares each bit of first operand to the  corresponding bit of second operand in two equal length operands.

Result bit will be ‘1’ if and only if both bits in the compared positions are ‘1’.

When we multiply any number by 0, output is always 0. same rule applies to And operator, whenever we multiply 1*1 result bit will be 1.

BitwiseAnd

Clear bit:  & operator is used to clear a particular bit in a number.

Eg. clear 2nd  bit in 46 (0 0 1 0 1 1 1 0 ) :

 

ClearBit

Not of 1 changes only expected bit in number and keeps remaining bits as it is.

 

  • OR ( | ) :

Binary Or operator compares each bit of first operand to the  corresponding bit of second operand in two equal length operands.

Result bit will be ‘o’ if and only if both bits in the compared positions are ‘0’.

BitwiseOr

Set bit :   As we saw & operator is used to clear a particular bit in a number, Or operator is used to set a particular bit in number.

Eg. Set 4th  bit in 46 (0 0 1 0 1 1 1 0 ) :

.   SetBit

This set bit method is used to efficiently store a number of Boolean values using little memory.

  • XOR ( ^ ) :

Binary Xor operator compares each bit of first operand to the  corresponding bit of second operand in two equal length operands.

Result bit will be ‘1’ if and only if either of the bits in the compared positions are ‘1’. That means if both bits are 1 then result will be 0.

XOR

Any bit can be toggled when XORed with 1.

Xor is  used in a tricky way to swap to integer variables without any extra space.

eg.  int a = 3 , b=9.      a:  0   0   1  1

b : 1   0   0  1

a= a ^ b         (a will be    1   0  1  0)

b= a ^ b        ( b will be   0  0  1   1)  which is value of a,  So now b=3

a=  a ^ b        (a will be     1  0  0   1)  which is value of b, So now a =9

  • Left Shift ( << ) :

Left shift operator shifts bits in first operand to left by number of bits given in second operand and inserts 0 bits are filled in vacant bits.

Eg. 23 << 1   :    0  0   0  1  0  1  1  1 << 1   result is :  0  0  1  0  1   1  1  0

  • Right Shift ( >> ) :

Right shift operator shifts bits in first operand to right by number of bits given in second operand and inserts 0 in vacant bits for unsigned numbers and for unsigned numbers  logically signed bit is copied as it is in most significant left bit and then 0 inserted in vacant bits. In C++, singed bit is copied in all the vacant bits.

Eg.  14 >> 2  : 0  0  0  0  1  1  1  0  >> 2  result is:  0  0   0   0   0  0  1  1

Tricks :

1. 0 1 1 1 + 0 1 1 1 (7+7) is equivalent to 0 1 1 1 * 2 (1 1 1 0 : 14), which is equivalent to shifting input number left by 1.  0 1 1 1 << 1 = 1 1 1 0 (14)

2. If a number is XOR with its own negated value, result will be sequence of 1s.  Eg. 0 1 1 1 ^ (1 0 0 0 ) = 1 1 1 1 . i.e. 7 ^(~7)

3. If we multiply any number by  2, simple way is to left shift that number by n. Eg. (3 * 4)  0 0 1 1 * 0 1 0 0  :: 4 =22

so 0 0 1 1 << 2 : : 1 1 0 0