Comments on: Turn off the rightmost bit of an integer https://demo.ritambhara.in/turn-off-the-rightmost-bit-of-an-integer/ Coding / System Design Interviews Wed, 15 May 2013 06:11:14 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Kamal Rawat https://demo.ritambhara.in/turn-off-the-rightmost-bit-of-an-integer/#comment-1728 Wed, 15 May 2013 06:11:14 +0000 http://www.ritambhara.in/?p=347#comment-1728 In reply to vishwa.

No.. It should work for all positive values of n.
if n == 2 (000…010)
then n & (n-1) will be (000…010) & (000…001) = 000…000 (hence unset the rightmost bit.
Similarly if n is one then also it will unset the rightmost bit (the only set bit).

]]>
By: vishwa https://demo.ritambhara.in/turn-off-the-rightmost-bit-of-an-integer/#comment-1727 Wed, 15 May 2013 05:47:06 +0000 http://www.ritambhara.in/?p=347#comment-1727 hi…
your code works perfectly only for values where n>2,
am i right…

]]>