简体   繁体   English

如何比较两个二进制数的位组?

[英]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.以十进制形式输入非负 32 位 (unsigned int) 和非负 8 位数字 (unsigned int)。 Determine how many times does the 8-bit number repeats in the first 32-bit number.确定 8 位数在第一个 32 位数中重复多少次。

I'm programming in C language and I'm having hard time understanding how to do it.我正在用 C 语言编程,我很难理解如何去做。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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