I am a java user. Just trying to firgure out this expression in C++

The following two code samples, written in the C++ programming language, both determine if the given unsigned integer x is a power of two.

// The obvious method
unsigned int x = ...;
bool isPowerOfTwo;
if (x > 0) {
while ((x % 2) == 0) {
x = x / 2;
isPowerOfTwo = (x==1);
isPowerOfTwo = false;

// A method using bit manipulation
[b] bool isPowerOfTwo = x && !(x & (x - 1));[/b]

Regarding the last assignment to isPowerOfTwo:

1. Presuming (x & (x - 1)) will evaluate to an int , s ! equivalent to ~ in this context?
2. why int values,x and , appear as operands of &&? How is the value of an (intValue1 && InValue2) expression determined.

I was looking at a wiki page about bit manipulation. And this confusion is really hindering my understanding of the whole thing, please help.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!