VOOZH about

URL: https://www.geeksforgeeks.org/dsa/short-note-on-bit-manipulation/

⇱ Short Note on Bit Manipulation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Short Note on Bit Manipulation

Last Updated : 24 Feb, 2026

Bit manipulation is the technique of performing operations directly on the binary representation of integers. Since computers store data in binary form (0 or 1 ), bitwise operations are extremely fast and memory-efficient.
The technique is widely used to perform binary operations efficiently, such as checking whether a number is odd or even, testing if a number is a power of two etc.

Common Bitwise Operators :

I. Bitwise And Operator ( &)

  • Sets each bit to 1 if both bits are 1 .
  • Some common uses are to check if a number is even or odd, ith bit is set or not etc.

Example - 5 & 3 → (00101)2 & (00011)2 → (00001)2 → 1

II. Bitwise OR Operator ( | )

  • Sets each bit to 1 if at least one bits are 1.
  • It is mainly used to set specific bits without affecting the others.

Example - 5 | 3 → (00101)2 | (00011)2 → (00111)2 → 7

III. Bitwise XOR Operator ( ^ )

  • Sets a bit to 1 if the corresponding bits are different.
  • It is widely used for finding unique elements, swapping two numbers without extra space etc.

Example - 5 ^ 3 → (00101)2 ^ (00011)2 → (00110)2 → 6

IV. Bitwise NOT Operator ( ~ )

  • Flips all the bits of a number, converting 1s to 0s and 0s to 1s.
  • It is useful when we need the complement of a number or while clearing bits using masks.
šŸ‘ truth_table_of_bitwise_not_operator

Example - ~5 → ~ (00...00101)2 → (11...11010)2

V. Left Shift Operator ( << )

  • Shift a bit to left , adds 0s to its right.
  • Equivalent to multiply by 2 .
  • It is often used for fast multiplication by powers of two.

Example - (21 << 1)→ ( (10101)2 << 1) → (01010)2 → 10

šŸ‘ logical_left_shift


VI. Right Shift Operator ( >> )

  • Shift bits to the right.
  • Equivalent to dividing by 2 .
  • It is used for fast division by powers of two.

Example - (21 >> 1)→ ( (10101)2 >> 1) → (01010)2 → 10

šŸ‘ logical_right_shift

Built-in Bit Functions :

Basic Practice Problems on Bitwise Algorithm :

Note: All the Bitwise Practice Problems are optimized and run in O(1) Time Complexity with O(1) Auxiliary Space.

I. Set the i-th Bit in an Integer

  • First, we left shift 1 to i-th position via ( 1<<n ).
  • Then, use the "OR" operator to set the bit at that position.

II. Unset/Clear the i-th Bit in an Integer

  • Left shift 1 by n positions using (1 << n) to create a mask with a 1 at the i-th bit.
  • Apply bitwise NOT ( ~ ) to the mask to make the nth bit 0 and all other bits 1.
  • Perform bitwise AND num & mask.
  • Since AND with 0 clears the bit and AND with 1 keeps it unchanged, the nth bit of num gets unset (cleared).

III. Toggling the i-th Bit of an Integer

  • Left shift 1 by n positions using (1 << n) to create a mask with a 1 at the i-th bit.
  • Then take the Xor ( ^) with of the mask with the number.

IV. Count Set Bits in an Integer

  • You can use a built-in function or iterate through a loop to count the number of bits.

V. Check if a Number is Power of 2 or Not

  • If N is a power of 2, then N & (Nāˆ’1) =0
  • This condition is also true for N=0.
  • Final check: n && !( n & ( n-1 ));

Application of Bit Manipulation :

šŸ‘ finding_unique_elements


Comment
Article Tags:
Article Tags: