techiehub.in

Bitwise Cheat Sheet

Bitwise OR (|)

if either of the bits is 1, it gives 1, else it shows 0

Bitwise AND (&)

if both bits are 1, it gives 1, else it shows 0.

Bitwise XOR(^)

if corresponding bits are different, it gives 1, else it shows 0.

Bitwise Complement(~)

with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.

Bitwise double shift right (>>)

shifts the bits of a given value to the right by a specified number of positions, fills the leftmost bits with the sign bit of the original value.

Bitwise tripe shift right (>>>)

shifts the bits of a given value to the right by a specified number of positions, filling the leftmost bits with zeros

result = value >>> numBits;

Bitwise double shift left (<<)

shifts the bits of a given value to the left by a specified number of positions.

Helpful Bitwise Operations

  • Erase the lowest set bit in a word in a single operation: x = x & (x-1)
  • Extract the lowest set bit of x: x & ~ (x-1)
  • Flip the bit least bit: x ^ 0x01

Categories