We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Flipping bits
Flipping bits
Sort by
recency
|
773 Discussions
|
Please Login in order to post a comment
A simple attempt using C++ bitset
Very simple solution: First perform NOT operation then Mask the number with 32-bit 1's (0xFFFF_FFFF) def FlippingBits(n): a = ~n & 0xFFFFFFFF return a
java(8)
Here is my c++ solution, you can watch the video explanation here : https://youtu.be/eZ0lTIzOjhQ
V1
V2
No need for complex code. Flipping bits meaning is to just get the biggest number in that requested bit and then substract those with the number asked in the question. For ex. : the question wanted a 32 bit, so the max number would be 4294967295. Now to get what the flipping bits of lets say 10 in 32 bit and return it to decimal, just do 4294967295 - 10 which is 4294967285, and there you go the decimal version of its flipping bit.
Here is in Go