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.








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.
Yes, correct. For non-negative numbers,
x >> kworks like division by2^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 = -1while-3 / 4 = 0Thanks for mentioning this. The post was mainly written for beginners, so I tried to keep the explanations simple.
are you auto infinity or what???
Na Bhai :)
it's actually
zero to infinityIt is well defined since C++20.
Nice Content
Thanks a lot. I am glad you liked the Content.
Great Content
Thanks a lot. I am glad that you found the Content useful.
thats almost 2 bites
Haha that was the goal actually :))
Helpful content
Thanks
more suggestions to increase accuracy and speed to solve bitwise operator questions? i struggle a lot in these type of questions
You should collect the Problems first then solve one by one. I tried this and it helps me the most.
https://codeforces.com/problemset?order=BY_RATING_ASC&tags=bitmasks%2C800-1400
Try to solve these problems of each rating from 800 to 1400.
thanks a lot hope so will reach specialist soon
I checked your CF profile & you already have good potential. Just keep practicing consistently and focus on understanding patterns deeply. Hope to see you reach Specialist very soon, and even beyond that. Keep it up!
Power of '0' & '1'
ig :)
Yeap :)
Auto comment: topic has been updated by 0t0infinity (previous revision, new revision, compare).
Useful
Thank you.
cool blog. U got my like and comment to share up!
Thanks a lot ! Really appreciate the support :)
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 !!
Now it feels like all the effort was worth it :)
Your support makes all the hard work worth it & Now the blog feels complete !
When we want to check if a number n is a power of 2, can we use the __builtin_popcount(n) function?
Absolutely! That’s actually one of the cleanest way.
If
__builtin_popcount(n) == 1thennis a power of 2.Just one thing to keep in mind — inside very tight loops
n & (n - 1)is usually preferred in CP since it’s a direct bitwise operation and slightly faster.Thanks for the tip about bitwise operation! Orz
Thanks for the clean explanation. It was really helpful.
I am glad that you found the content useful.
wtf strong
Tried my best :)
Glad you liked it !
cmnting to save
Glad to hear !