C++ Int位操作是2UL = 10UL?

Oru*_*aru 0 c c++ bit-manipulation

我有一个快速的问题.

我一直在c/c ++中使用位操作一段时间,我最近发现当我将2UL和10UL与常规的无符号int进行比较时,它们似乎返回相同的位.

例如,

#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);
}
Run Code Online (Sandbox Code Playgroud)

请确认2U是否等于10U,如果是,如果我需要一个无符号整数中的8(?)个标志,我将如何绕过它.

亲切的问候,

-Markus

jpa*_*cek 5

当然.10ul是二进制1010,2是10.因此,也x |= 10设置第二位.

你可能想用0x100x2作为你的旗帜.这些将按预期工作.

另外:十六进制表示法中的单个数字代表4位,而不是8位.