Hacker News new | ask | show | jobs
by revelation 4674 days ago

    bool a = value & (1 << 5)
a will be 1 or 0, not 1 << 5. You don't get this behavior with a normal int. MSVC also has a warning about some of this behavior [1], with a nonsense performance subtext. I don't think theres a GCC equivalent.

1: http://msdn.microsoft.com/en-us/library/b6801kcy.aspx

2 comments

Seems like the only reason you'd expect it to be 1 << 5 is that you've been working with a broken definition of bool using #define.

In any sane language you can't redefine bool that way, nobody would ever expect bool to take more than two values, and there wouldn't be a problem.

But, this is the case in every other language I can think of. IE, bools have one of two possible states; as a human 1<<5 is neither true nor false.