Page 1 of 1

C question

Posted: Mon Dec 22, 2008 3:31 pm
by renovatio

Code: Select all

If(N & 0x0001)
What is that condition checking?

Thanks :)

Re: C question

Posted: Mon Dec 22, 2008 3:44 pm
by ipsemet
It's checking to see if that bit is set.

So if N = 0x0010, it would be false.

Re: C question

Posted: Mon Dec 22, 2008 4:01 pm
by renovatio
Thanks. That code is used to see if a number is odd or even, but how does it work?

thanks again.

Re: C question

Posted: Mon Dec 22, 2008 4:07 pm
by ipsemet
If the lowest bit is not set, then the number is divisible by 2 (binary)

If the lowest bit is set, then the number isn't divisible by 2.

So if that code returns true, then the number is odd.

Example:
N = 0xA
0xA in binary = 1010b
OxA in decimal = 10
Since the last bit isn't set, the number is even.

Re: C question

Posted: Mon Dec 22, 2008 4:15 pm
by renovatio
thanks ;)

Re: C question

Posted: Tue Dec 30, 2008 12:17 am
by Rook
It's the same as writing:

Code: Select all

if( N % 2 != 0 ) // if( there is a remainder when N is divided by 2 ); i.e. if( n is odd )
which is arguably far clearer and would be better to write, despite boolean operations being faster: any good optimising compiler would presumably produce the statement you have given.

Edit: of course N could be anything (you haven't specified what it is - I just assumed that it was an optimisation of the above, as using the letter N has certain connotations), and strictly speaking the above would not be clearer if N was some bitfield, in which case your code would be more clearly interpreted as simply checking if the least significant bit of N is set.

Re: C question

Posted: Tue Dec 30, 2008 5:34 am
by jal
renovatio wrote:

Code: Select all

If(N & 0x0001)
What is that condition checking?
Although the right answer has been given a few times, I'd like to make it slightly more informative, as there are two things at work here.

1) The N & 0x0001 statement itself performs a bitwise, binary AND, that is, it takes the two operands (N and 0x0001) and performs, for each bit, an AND. Since the AND operation returns 0 if either bit is 0, and 1 only when both bits are 1, it is easy to see only the very 'last' bit (the least significant one) can ever result in 1, since the second operand is a constant (viz. 1). So if the least significant bit of N is 1, the result of N & 0x01 is 1, otherwise it is 0. Since, if the lowest bit of a number is set, the number is odd, otherwise it's even, this can be used to test for odd/eveness.

2) Although C has a set of comparative operators, viz. <, <=, ==, !=, => and >, and although you will often see these used in an if-statement, C does not need these operators in an if-statement. What C does is evaluate the entire expression between parentheses, and executes the code after the if-statement in case the result of the expression does not equal 0. If it equals 0, it does not execute that code, but instead the code after the else (if present). This is different from what other languages do/allow (i.e. many languages need at least one comparative operator after an if). In fact, the C comparative operators also return 0 or 1, depending on the outcome of the comparison.


JAL

Re: C question

Posted: Tue Dec 30, 2008 8:40 am
by CodeCat
C has no boolean type, so it uses int instead. In C++, which does have a boolean, you can still do the same though as int is converted to bool automatically.

Re: C question

Posted: Tue Dec 30, 2008 9:52 am
by quok
CodeCat wrote:C has no boolean type, so it uses int instead. In C++, which does have a boolean, you can still do the same though as int is converted to bool automatically.
Bah, C99 has _Bool, which is just a two value int. So don't be too surprised if sizeof(_Bool) returns 4. :) Anyway, have a look at stdbool.h.

Granted, true and false are just #defines that are set to the integer constants 1 and 0. Some implementations may incorrectly cast them to (_Bool)1 and (_Bool)0, but even sizeof(true) returns 4 due to C's rules regarding literal types.

I guess you could say this means _Bool isn't a true boolean type, and I suppose depending on how you look at it you may be correct, but C does have a boolean type these days. Btw, stdbool.h is defined in section 7.16 of the C99 standard, and _Bool is defined in section 6.2.5.

Re: C question

Posted: Tue Dec 30, 2008 11:31 am
by CodeCat
I'm not sure what the C++ standard says on bools, but they work pretty much like integers there too. They're a built-in type though, not just typedefs. AFAIK you can do:

Code: Select all

bool foo = 5;
int bar = 2*foo;
and it should compile. However, I've no idea what the value of bar is afterwards. If foo is a true boolean, then 5 should be casted to 'true', and 2*true would equal 2. But I haven't tested this.

Re: C question

Posted: Wed Dec 31, 2008 5:34 pm
by Love4Boobies
This topic should be locked, IMO.

Re: C question

Posted: Fri Jan 02, 2009 8:38 am
by jal
berkus wrote:
CodeCat wrote:If foo is a true boolean, then 5 should be casted to 'true', and 2*true would equal 2. But I haven't tested this.
You are correct.
It's not casting though. '5' is evaluated to 1 as a conditional expression. "if (5)" is always true, and I'm pretty sure (though I'm to lazy to test) it yields the same code as "if (5 == 5)" (if not optimzed to heavily).
However. A little bit of weirding ways of casting reveals that it's still an integer type
It is, and that's not weird, but defined that way. A bool is a 1 byte integer that can hold the values 0 and 1.
(unlike the boolean type in e.g. Pascal which is usually being optimized into a bitfield by the compiler):
I think you are confusing it with sets, which are encoded as bitfields. It would be weird to optimize a single boolean as a bitfield (wouldn't call that optimizing). Though indeed, iirc, you cannot assign integers to booleans in Pascal, or vice versa.
ps/ and hello Eindhoven, nice city, liked there a lot :)
Eindhoven can be quite nice, although I hear mixed stories about whether it's a nice place to live...


JAL

Re: C question

Posted: Mon Jan 05, 2009 6:54 am
by jal
berkus wrote:I haven't played with Pascal for a long while already, but the Borland Pascal 7.0 I think stuffed booleans into a bit array.
You mean it stuffed multiple booleans into a single integer? I could see that, although it's still pretty weird...


JAL

Re: C question

Posted: Mon Jan 05, 2009 6:59 am
by Solar
When discussing weird things Pascal does with booleans, consider C++ vector<bool>... (which, strictly speaking, isn't even an STL container...)

Re: C question

Posted: Mon Jan 05, 2009 7:21 am
by jal
Solar wrote:When discussing weird things Pascal does with booleans, consider C++ vector<bool>... (which, strictly speaking, isn't even an STL container...)
Indeed, vector<bool> is evil.


JAL