Tuesday, January 3, 2012

Toggle / Isolate Bits of anumber

1.Toggle kth bit
2.Toggle rightmost one bit
3.Isolate rightmost 1 bit
4.Isolate rightmost zero bit




2 comments:

  1. n ^ (1 << (k-1)) toggles the kth bit

    n & (n-1) toggles the rightmost one bit

    n & -n isolates rightmost one bit [ -n is two's complement of n]

    ~n & (n+1) isolates rightmost zero bit

    ReplyDelete
  2. A little bit get inside.... may i know why are you applying only such operation(logic behind bit properties).... bcz if you need to augment any algorithm then you required it. pls

    ReplyDelete