12 c++ java bit-manipulation rotation
给定两个整数a和b,我们如何检查b是旋转版本?
例如,如果我a = 0x01020304(二进制0000 0001 0000 0010 0000 0011 0000 0100),则以下b值是正确的:
0x4080C1 (右旋2) 0x810182 (右旋1)0x2040608 (左转1)0x4080C10 (左转2)在C++中,没有字符串转换并假设32位int:
void test(unsigned a, unsigned b)
{
unsigned long long aa = a | ((unsigned long long)a<<32);
while(aa>=b)
{
if (unsigned(aa) == b) return true;
aa>>=1;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)