You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Number of set bits -> while (x!=0) { x = x & (x-1)}
x-1 flips all the bits till (including) the rightmost set bit.
a+b = a^b + 2(a&b)
Swap two numbers -> a = a^b; b = b^a; a= a^b;
Flip the bits of number -> (Have number with all set bits) ^ number
This isn't strictly related to bit-magic but there are questions like conversion of string to integer where you need to figure out if your next digit is going to cause an overflow. There use this answer on stackoverflow (https://stackoverflow.com/a/13661672/648290). Pay special attention to the negative case. An implementation of this can be found at https://leetcode.com/problems/string-to-integer-atoi/