Bryan wrote:
is to reset the rightmost (less significant) '1' bit of a number (ie change it to '0').
i tried c &= c - 1 but i'm not getting the least significant or rightmost bit reset to zero. am i misunderstanding something?
2 & 1 # 2 = 0x10; reset right most would be 0x10
0
10 & 9 # 10 = 0x1010; reset right most would be 0x1010
8
The difference between the original "reset the rightmost '1' bit", and your interpretation: "reset the rightmost bit" is the "'1'".
The rightmost bit that is set is reset. So 0x10 -> 0, and 0x1010 -> 0x1000.
If you want to extract the least significant set bit from a number 'x' you can use (x&-x):
x = 0xab4 while x:
print hex(x&-x), hex(x) x ^= (x&-x)
0x4 0xab4 0x10 0xab0 0x20 0xaa0 0x80 0xa80 0x200 0xa00 0x800 0x800
(but don't try this if x is negative: it works but never terminates).
thanks duncan... you're right, i did intrepret this as "reset the rightmost bit" instead of "reset the rightmost '1' bit". and i must have read what christos wrote 100 times!!!
bryan
-- http://mail.python.org/mailman/listinfo/python-list