You are viewing a single comment's thread. Return to all comments →
Java Solution | One Liner ✅
public static long flippingBits(long n) { return (long) (Math.pow(2, 32) - 1)^n; }
Integer.MAX_VALUE gives 2147483647 which is (2^31 - 1) and is not 32 bit, it is only 31 bit as 1 bit is reserved for sign.
(long) (Math.pow(2, 32) - 1) gives 4294967295 which is (2^32 - 1) and is 32 bit, so if we xor this with n we get flipped number
Seems like cookies are disabled on this browser, please enable them to open this website
Flipping bits
You are viewing a single comment's thread. Return to all comments →
Java Solution | One Liner ✅
Integer.MAX_VALUE gives 2147483647 which is (2^31 - 1) and is not 32 bit, it is only 31 bit as 1 bit is reserved for sign.
(long) (Math.pow(2, 32) - 1) gives 4294967295 which is (2^32 - 1) and is 32 bit, so if we xor this with n we get flipped number