Access bits in a char in C

前端 未结 5 1629
别跟我提以往
别跟我提以往 2020-12-09 18:10

I have a hex number 0x37 and its binary representation is 0011 0111. How do I access the first 2 bits of the binary representation which is \"11\"? How do I use bit shifting

5条回答
  •  生来不讨喜
    2020-12-09 19:10

    Here is a sample to access it bit by bit:

    #include 
    int main()
    {
        char byte = 0x37;
        int i;
    
        for(i = 7; 0 <= i; i --)
            printf("%d\n", (byte >> i) & 0x01);
    
        return 0;
    }
    

提交回复
热议问题