0t0infinity's blog

By 0t0infinity, history, 5 months 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 support 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)

Part 2 (Intermediate Level) : Part-2 — Click Here


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

»
5 months 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.

»
5 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Nice Content

»
5 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Great Content

»
5 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

thats almost 2 bites

»
5 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Helpful content

»
5 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

more suggestions to increase accuracy and speed to solve bitwise operator questions? i struggle a lot in these type of questions

»
5 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Power of '0' & '1'

ig :)

»
4 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Updated !

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Useful

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

cool blog. U got my like and comment to share up!

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

Truly impressive work... It's rare to find such a clear, well structured and concise explanation of bitwise tricks with examples. May your hard work pay off !!

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

When we want to check if a number n is a power of 2, can we use the __builtin_popcount(n) function?

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Thanks for the clean explanation. It was really helpful.

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

wtf strong

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

cmnting to save

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

This is a good binary blog.

But Part 1 is too too too too easy.....

orz

  • »
    »
    4 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    Exactly, that was actually intentional.
    I wanted Part 1 to be fully beginner friendly for people who are completely new to bits and binary concepts.
    Part 2 will go into more intermediate-level tricks & the final part 3 will focus on the actual harder CP oriented ideas.
    Still, really glad you enjoyed it & liked.

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Waiting for Advanced Part!!

»
4 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

I think The Next Part should be on Topics not just Tricks !

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

up

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Wow, Great content i was studying bits for a while,I just know some basics and this is helpful waiting for the next part

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Thanks a lot, really nice educational information for all levels :)

»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by 0t0infinity (previous revision, new revision, compare).

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Hey! I really liked this blog, been reading it since yesterday. I got to learn some really cool new things, and I really appreciate the effort you put into writing this. Also, this reminded me of another trick for setting the lowest unset bit which is x | (x + 1).

Always gonna be a fan of bit manipulation. I have replaced modulo with numbers which are power of 2 with bitwise AND which is x % 2^n = x & (2^n - 1). Heading to read the intermediate one now :)

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    Thanks man for reading and sharing these brilliant tricks :)
    using x | (x + 1) x & (2^n - 1) is great but just keep this in mind :-

    1. For x | (x + 1) : Be careful if $$$x$$$ is already at its maximum value (INT_MAX) then x + 1 will cause a signed integer overflow.

    2. For the Modulo Trick : This works perfectly only for positive numbers.
    Like :- -5 % 4 gives -1 but -5 & 3 will give 3 this might lead to a WA ! also dont forget to use proper parentheses due to operator precedence like x & ((1 << n) - 1).

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

is there a way to mark this blog for future reference? cause this is hella useful.

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    Yeap, check there is a star button next to the voting button for this blog... click on the button, and this blog will be added to your favorite list. you will find the favourite list on you profile later.

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Glad you found it useful ! yeah man, to make it favorite -
    follow the instructions in RxSoul's comment.
    thanks ubub RxSoul for your help <3

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

needed content thanks

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Thanks for this , bit problems were cooking me good. Now I can finally retaliate lol.

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it
  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    This is massive man ! but insightful infos ! its actually in 2 parts first one is Gospers Hack & The second part is Combinatorial DP. Since you asked for an explanation of how this works -
    let me break down the first part (nssbn) :-

    Gospers Hack (Next mask with the exact same number of set bits) :-

    Lets understand how this $$$O(1)$$$ magic works line by line. Suppose our current mask is x = 22 (Binary: 010110). It has exactly 3 set bits. We want to find the next smallest number that also has exactly 3 set bits.

    • ll right = x & -x;
      This is our classic trick ! It extracts the lowest set bit (rightmost 1).
      For x = 010110, right becomes 000010.
    • ll higher = x + right;
      Adding right to x causes a carry ripple. It turns the rightmost continuous block of 1s into 0s and flips the next available 0 on the left to a 1. 010110 + 000010 = 011000.
      Great! we moved the block to the left ! But wait — in doing that we lost the remaining 1s from that block. We need to restore them, but place them as far right as possible to keep the number strictly the next smallest.
    • return higher | ((x ^ higher) / right) >> 2;
      This line does the exact mathematical cleanup to restore those lost 1s at the very end.
    • x ^ higher isolates the bits that changed (001110).
    • Dividing by right shifts these changed bits to the rightmost position.
    • >> 2 exactly trims out the extra bits caused by the carry, leaving us with the exact number of 1s we need to restore.
    • Finally, | merges our higher value with these adjusted 1s.
      Result: 011000 | 000001 = 011001 (which is 25). The Magic works flawlessly !

    As for the second part (Lexicographical DP for finding the $$$K$$$-th next/prev mask) — to be honest that is still too hard for me ! I am still in the learning phase and havent mastered this kind of advanced combinatorial DP yet !


»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

This really helped a lot. Thank you