Code: Select all
If(N & 0x0001)
Thanks
Code: Select all
if( N % 2 != 0 ) // if( there is a remainder when N is divided by 2 ); i.e. if( n is odd )
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.renovatio wrote:What is that condition checking?Code: Select all
If(N & 0x0001)
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.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.
Code: Select all
bool foo = 5;
int bar = 2*foo;
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).berkus wrote:You are correct.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.
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.However. A little bit of weirding ways of casting reveals that it's still an integer type
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.(unlike the boolean type in e.g. Pascal which is usually being optimized into a bitfield by the compiler):
Eindhoven can be quite nice, although I hear mixed stories about whether it's a nice place to live...ps/ and hello Eindhoven, nice city, liked there a lot :)
You mean it stuffed multiple booleans into a single integer? I could see that, although it's still pretty weird...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.
Indeed, vector<bool> is evil.Solar wrote:When discussing weird things Pascal does with booleans, consider C++ vector<bool>... (which, strictly speaking, isn't even an STL container...)