[英]C - How to check if 8 bits are in 32 bit?
I want to check if the 8 bits in a char
are a substring of the 32 bits of an int
. 我想检查
char
中的8位是否是int
的32位的子串。
a = 0110 1010 1011 0100 0000 0110 1010 0010 (32 bit int)
b = 0100 0000 (8 bit char)
is_in(a, b) --> true
Here is my code: 这是我的代码:
for (int i = 0; i < 25; i++) {
int tmp = a;
tmp <<= 24;
tmp >>= 24;
int res = b ^ tmp;
res <<= 24;
res >>= 24;
if (res == 0)
return 1;
else
a >>= 1;
}
return 0;
I want it more efficient. 我希望它更有效率。 Any idea?
任何想法?
Well, you could try... 好吧,你可以试试......
bool is_in(uint32_t a, uint8_t b) {
while (a >= b) {
if ((a & 0xff) == b) return true;
a >>= 1;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.