简体   繁体   中英

How to compare groups of bits of two binary numbers?

the coding question is

Enter non-negative 32-bit (unsigned int) and non-negative 8-bit number (unsigned int) in decimal form. Determine how many times does the 8-bit number repeats in the first 32-bit number.

I'm programming in C language and I'm having hard time understanding how to do it. So any help is welcomed. Thanks

int howMany(unsigned num, unsigned char byte)
{
    int result = 0;
    for(int sft = 0; sft <= (sizeof(unsigned) - 1) * CHAR_BIT; sft++, num >>= 1)
        if((num & UCHAR_MAX) == byte) result++;
    return result;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM