You're viewing a comment by John Doe and its responses.
You're viewing a comment by John Doe and its responses.
I am being sponsored by Syntress! They bought me an amazing dedicated server to run catonmat on. If you're looking web services, I highly recommend the Syntress guys!
I am being sponsored by A-Writer! If you ever need help with essay writing, look no further than A-Writer! They will help you with your writing in as quickly as 3 hours!
I love to read science books. They make my day and I get ideas for awesome blog posts, such as Busy Beaver, On Functors, Recursive Regular Expressions and many others.
Take a look at my
Amazon wish list, if you're curious about what I have planned reading next, and want to surprise me. :)
If you are interested in advertising on catonmat.net, contact me.
Free tools for coding on Vietstarsoft.com.
Programming homework help.


Sorry to be a party crusher, but you've got a mistake (at least one, I stopped reading.)
=========================================
Bit Hack #6. Turn off the rightmost 1-bit.
y = x & (x-1)
=========================================
Plain wrong. Any X ending with xxx10, when ANDed with (x-1), which obviously ends with xxx01, gives xxx00, ie. a number whose LAST TWO BITS ARE 0.
2d & 1d = 0
10d (0xA) & 9d (0x9) = 8.
Sorry.
Comment Responses
Isn't the answer for Bit Hack #6 simply:
y = x & 0xFFFFFFE
(at least for 32-bit values)
I do not see any problem.
xxx10 => xxx00, so the rightmost bit is 0 after that, as advertised.
The problem is with the word 'rightmost' in the title. Like you, I initially assumed that it meant the final bit in the pattern; but what it is intended to mean is "Reset the first found bit that has a value of one when scanning from right to left", but that is a mouthful!
The affected bit in your example of 'xx10' is the '1' between the 'xx' and '0', which (as you correctly point out) is the bit that gets changed to 'xx00'. Rather than being an error, that is the intention of the hack.
I had to go back and read Bit Hack#6 again to check, but it DOES do what it says - it turns off the *rightmost 1 bit* (that is, the last binary '1' when reading left to right), which is not necessarily the *rightmost bit* of the number.
Reply To This Comment