I have a quick question.
I’ve been playing around with bit manipulation in c/c++ for a while and I recently discovered that when I compare 2UL and 10UL to a regular unsigned int they seem to return the same bit.
For example,
#define JUMP 2UL
#define FALL 10UL
unsigned int flags = 0UL;
this->flags |= FALL;
//this returns true
this->is(JUMP);
bool Player::is(const unsigned long &isThis)
{
return ((this->flags & isThis) == isThis);
}
Please confirm if 2U equals 10U and if so, how would I go around it if I need more than 8(?) flags in a single unsigned integer.
Kind regards,
-Markus
Of course.
10ulis 1010 in binary and 2 is 10. Therefore, doingx |= 10sets the second bit too.You probably wanted to use
0x10and0x2as your flags. These would work as you expect.As an aside: a single digit in the hex notation represent 4 bits, not 8.