Skip to main content

Bit Manipulation (IMP)



🔸 n & (n - 1)​

  • Unsets rightmost set bit (1)
  • Example: 12 (1100) → 8 (1000)
  • Use: Count set bits

🔸 n | (n - 1)​

  • Sets all bits to the right of rightmost unset bit
  • Example: 8 (1000) → 15 (1111)
  • Use: Fill lower bits with 1s

🔸 n & (n + 1)​

  • Unsets trailing set bits
  • Example: 7 (0111) → 0 (0000)
  • Use: Check if all bits are set → n & (n+1) == 0

🔸 n | (n + 1)​

  • Sets the rightmost unset bit
  • Example: 10 (1010) → 11 (1011)
  • Use: Set rightmost 0

🚀 Level Up Your Engineering Mindset

Never Study in Isolation. Join Binary Dose.

Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚡
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

💡Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→