簡體   English   中英

如何比較兩個二進制數的位組?

[英]How to compare groups of bits of two binary numbers?

編碼問題是

以十進制形式輸入非負 32 位 (unsigned int) 和非負 8 位數字 (unsigned int)。 確定 8 位數在第一個 32 位數中重復多少次。

我正在用 C 語言編程,我很難理解如何去做。 因此,歡迎任何幫助。 謝謝

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