Bit Manipulation

In computers everything is stored as a bit, in simple words 0 and 1, every integer every number is internally stored as a bit. 1 byte contains 8 bits.

Modern computer programing languages provide operations on a bit. Bit Manipulation is very fast and optimized the time complexity of an algorithm. There are multiple bitwise operators available for bit manipulation.

Bitwise Operators:

NOT ( ~ )
Bitwise NOT takes a single input parameter hence it's a urinary operator, it flips the bits of the number i.e. if the bit is 0, it will change it to 1 and vice versa. in other words, Bitwise NOT is one’s complement of a number.

AND ( & )

Bitwise AND takes 2 input parameters hence it's a binary operator. If 2 different bits patterns are 1, then AND of both bits is 1, otherwise 0.

& - If both bits are set bit/1, then only result bit is set bit otherwise unset bits

   X   

   &   

   Y   

   Op   

   0

   &

   0

   0

   0

   &

   1

   0

   1

   &

   0

   0

   1

   &

   1

   1

OR ( | )

Bitwise OR takes 2 input parameters hence it's a binary operator. If any of the 2 different bit patterns is 1, then the OR of both bits is 1, otherwise 0.

| - If any bits are set bit/1, then only result bit is a set bit

   X   

   |   

   Y   

   Op   

   0

   |

   0

   0

   0

   |

   1

   1

   1

   |

   0

   1

   1

   |

   1

   1

 

XOR ( ^ )

Bitwise XOR takes 2 input parameters hence it's a binary operator. If both bits of 2 different bit patterns are different, then the XOR of both bits is 1, otherwise 0.

^ - If both bits are the same then the result bit is 0 else if both are a different bit, then the result bit will give 1

a^0=a, a^a=0

   X   

   ^   

   Y   

   Op   

   0

   ^

   0

   0

   0

   ^

   1

   1

   1

   ^

   0

   1

   1

   ^

   1

   0

Left Shift ( << )

The left shift operator takes 2 input parameters hence it's a binary operator. it shifts the bits of a pattern to the left and appends 0 at the end. 

Right Shift ( >> )

The right shift operator takes 2 input parameters hence it's a binary operator. it shifts the bit of pattern to the right and appends 1 at the end.

follow us on