What's the best way to toggle the MSB?
问题 So I want to toggle the most significant bit of my number. Here is an example: x = 100101 then answer should be 00101 I have a 64 bit machine and hence I am not expecting the answer to be 100000..<51 0's>..100101 One way I thought of was to count the number of bits in my number and then toggle the MSB, but not sure on how to count. 回答1: The cheat is to pawn it off to the compiler: There are instructions in most CPUs for doing work like this. The following should do what you want. i ^ (1 <<