Prove that, if x AND x-1 = 0 then x=2^m (m>=0).



Hi all,

How to prove, if x AND x-1 = 0 then x=2^m (m>=0)?

Here the fact is, if we perform a logical AND operation of the bits of
x and x-1 (x and x-1 are two numbers stored in computer memory) and we
get a value zero, then we can say x is a number that is power of 2.

Srinu.

.



Relevant Pages

  • Re: test whether a number is a power of 2
    ... > If x is a power of two, it doesn't have any bits in common with x-1, since it ... > consists of a single bit on. ... Any positive power of two is a single bit, ... > This means that we test if x-1 and x doesn't share bits with the and operator. ...
    (comp.lang.c)
  • Re: An interesting problem
    ... at the end x-1 should be zero, but it turns out to be -1.1102e-016, WHY? ... that go into more depth on floating-point computation: ... Steve Lord ...
    (comp.soft-sys.matlab)
  • Re: An interesting problem
    ... at the end x-1 should be zero, but it turns out to be -1.1102e-016, WHY? ... use symbolic computation. ...
    (comp.soft-sys.matlab)