[英]C - How to check if 8 bits are in 32 bit?
我想檢查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
這是我的代碼:
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;
我希望它更有效率。 任何想法?
好吧,你可以試試......
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.