How do i get the lower 8 bits of int?

前端 未结 4 587
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 19:56

Lets say I have an int variable n = 8. On most machines this will be a 32 bit value. How can I only get the lower 8 bits (lowest byte) of this in binary? Also how can i acce

4条回答
  •  青春惊慌失措
    2020-12-08 20:14

    Use bitwise arithmetic to mask off the lowest 8 bits:

    unsigned char c = (x & 0xFF);
    

    To access the nth lowest bit, the equation is (x & (1 << n)) (n of zero indicates the least significant bit). A result of zero indicates the bit is clear, and non-zero indicates the bit is set.

提交回复
热议问题