You're viewing a comment by Jeremy and its responses.

Jeremy Permalink
July 02, 2009, 23:38

John Doe:

Both of your examples produce correct results with the algorithms presented in the article. For 0x42:

0x42:             0b101010
0x42 & (0x42 -1): 0b101000

So, the rightmost 1 bit has been cleared. Same for 0x2:

0x2:           : 0b10
0x2 & (0x2 - 1): 0b00

In both cases, the rightmost (ie, LSB) 1 bit has been set to 0, which is the correct behaviour.

As you say:

since the result of the expression is that BOTH the last two bits are reset.

But the last bit wasn't set in the input number, so it hasn't been cleared, it was 0 to start with. The algorithm's purpose is to clear the rightmost 1 bit.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "sandbox_143": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.