Grabbing n bits from a byte

后端 未结 6 2017
走了就别回头了
走了就别回头了 2021-01-30 14:31

I\'m having a little trouble grabbing n bits from a byte.

I have an unsigned integer. Let\'s say our number in hex is 0x2A, which is 42 in decimal. In binary it looks li

6条回答
  •  攒了一身酷
    2021-01-30 15:33

    just get rid of the 8* in your code.

    int input = 42;
    int high3 = input >> 5;
    int low5 = input & (32 - 1); // 32 = 2^5
    bool isBit3On = input & 4; // 4 = 2^(3-1)
    

提交回复
热议问题