10

Possible Duplicates:
How do you set, clear and toggle a single bit in C?
Removing lowest order bit

n is a positive integer. How can its rightmost set bit be unset?

Say n= 7 => n = 0111. I want 0110 as the output. Is there any simple bitwise hack to achieve the goal?

Community
  • 1
  • 1
Edward Norton
  • 101
  • 1
  • 3
  • related, with explanation, both get & unset. [How to get position of right most set bit in C](https://stackoverflow.com/questions/31393100/how-to-get-position-of-right-most-set-bit-in-c/42747608#42747608) – qeatzy Sep 16 '19 at 01:32

3 Answers3

19

Try n & (n-1) where & is bitwise AND

n = 7
n - 1 =6

n & (n-1)=> 0 1 1 1   (7)
          & 0 1 1 0   (6)
           --------- 
            0 1 1 0  (done!)

EDIT (in response to the comment given by Forest)

n = 6 
n - 1 = 5

n & (n-1)=> 0 1 1 0   (6)
          & 0 1 0 1   (5)
           --------- 
            0 1 0 0  (done!)
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
5

Your question is unclear.

If you just want to unset bit 0, here are some methods (with slight variations in behavior depending on your types involved):

x &= -2;
x &= ~1;
x -= (x&1);

If you want to unset the lowest bit among the bits that are set, here are some ways:

x &= x-1;
x -= (x&-x);

Note that x&-x is equal to the lowest bit of x, at least when x is unsigned or twos complement. If you want to do any bit arithmetic like this, you should use only unsigned types, since signed types have implementation-defined behavior under bitwise operations.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1
unsigned int clr_rm_set_bit(unsigned int n)
{
    unsigned int mask = 1;
    while(n & mask) {
        mask <<= 1;
    }
    return n & ~mask;
}
Joe W
  • 21
  • 1
  • 1
    This is O(N) while Prasoon's is O(1). Also, you want something more like `while(!(n & mask))`, but even that doesn't work for `n = 0`. – Mike DeSimone Jan 16 '11 at 05:47
  • 1
    Actually this method (when correctly implemented) isn't `O(n)`. It's `O(log(n))`. And since n is bounded by the size of an unsigned int, you could also call it O(1). – Artelius Jan 16 '11 at 05:56
  • Right, should have bounds checked for n==0 and n < MAXINT (or whatever the correct constant is for that) At least I interpreted the question correctly – Joe W Jan 16 '11 at 05:56