I want to check if the 8 bits in a char
are a substring of the 32 bits of an int
.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.