Is this the correct expression?

I have to write a C expression that yields 1 for the condition described and 0 for otherwise. X is an integer
. Any bit of x equals 1

is !!X right?
That would probably work by virtue of the fact that the only number that is all 0 bits is 0 itself, which would translate into a boolean false. However it would return true or false; not 1 or 0. Although I think an ostream automatically converts them to such...
Last edited on
Zhuge: Okay, if the values of true and false ever change to anything other than "not zero" and "zero" (such as "one" and "not one"), I'm sure OP will care then.

Yes, your expression is correct. X!=0 also works.
Last edited on
What a strange question. Wouldn't X alone work?

1
2
3
4
5
6
7
8
if( X )
{
    // some bit is 1
}
else
{
    // x is 0
}
But if X is !1 and !0, the result will be incorrect:
I have to write a C expression that yields 1 [...] [or] 0.
closed account (z05DSL3A)
I was just wondering if the actual text of the OPs 'assignment' was to write a Conditional Expressions...along the lines of Y = (X != 0 ) ? 1 : 0;.
ok, not sure if matters but I was supposed use bit level & logical operationin answering the questions.

So I have another question: Again in C. any bit in the least significant byte of x is equal to 1. the answer would be !! (X & 0xF)?
Topic archived. No new replies allowed.