0t0infinity's blog

By 0t0infinity, history, 4 hours ago, In English

Bitwise Operations was one of the most confusing topics for me at first. But once I started understanding how bits actually work, many CP problems suddenly became much easier to think about and optimize.

This blog is the result of a full day of grinding, where I tried to organize everything I know about bitwise tricks into a clear and structured form — from the basics to useful CP patterns. I tried to keep everything as simple and beginner-friendly as possible so that anyone can understand the core idea behind each trick instead of memorizing formulas. If even one trick from this blog helps someone solve a problem faster or understand bits more clearly, then this effort feels truly meaningful for me.

And finally, I want to express my deepest gratitude to Raha Ahmed for being The Strongest supports in my CP journey. Her constant encouragement, belief and presence have played a huge role in shaping my progress and I’m truly grateful for everything she has done.


The Basics for Beginners :


Basics All - (click here)

Trick 1 : Check Odd or Even Number


Explanation - (click here)

Trick 2 : Check The k-th bit


Explanation - (click here)

Trick 3 : iterate Through All Bits


Explanation - (click here)

Trick 4 : Count Set bits / On bits


Explanation - (click here)

Trick 5 : Set and Clear The k-th bit


Explanation - (click here)

Trick 6 : Toggle The k-th bit


Explanation - (click here)

Trick 7 : Multiply and Divide by 2


Explanation - (click here)

Trick 8 : Remove The Lowest Set bit


Explanation - (click here)

Trick 9 : Get The Lowest Set bit


Explanation - (click here)

Trick 10 : Count Trailing and Leading Zeros


Explanation - (click here)

Trick 11 : Find The Highest Set Bit Position


Explanation - (click here)

Trick 12 : Check whether A Number is Power of 2


Explanation - (click here)

Trick 13 : Find The Unique Element using XOR


Explanation - (click here)

Trick 14 : Generate All Subsets using Bitmask


Explanation - (click here)

  • Vote: I like it
  • +7
  • Vote: I do not like it

»
40 minutes ago, hide # |
Rev. 2  
Vote: I like it +1 Vote: I do not like it

For division by right shift there is a big gotcha. It would always work for unsigned int x, or for non-negative int. For negative ints it's up to implementation of compiler. In practice it's usually arithmetic shift, so 31th bit would be copied in every bit position shifted in from the left, so negative numbers remain negative. But then division result is not correct when it should result in zero. For example, (-3)>>2 would give not zero but -1. Anyway compilers would optimize division by constant power of two.

  • »
    »
    33 minutes ago, hide # ^ |
    Rev. 8  
    Vote: I like it +1 Vote: I do not like it

    Yes, correct. For non-negative numbers, x >> k works like division by 2^k.
    But for negative integers, right shift behavior is implementation-defined in C++.
    Most compilers use arithmetic shift, so sign bits get copied.
    Example: (-3) >> 2 = -1 while -3 / 4 = 0

    Thanks for mentioning this. The post was mainly written for beginners, so I tried to keep the explanations simple.

»
31 minute(s) ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Nice Content