What does least significant byte mean?

后端 未结 4 1077
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 19:43

I have to implement a method that writes a byte to an ostream object. Let\'s just called this ostream object strobj. I also have a bit

4条回答
  •  情深已故
    2020-12-09 20:34

    Imagine a 32-bit integer, where each bit can be 0 or 1, say:

    011101010101010101010101010110110
                             ^^^^^^^^
    

    The least significant byte is the 8-bits at the right-hand side.

    It's probably easier to understand if you take a decimal example: given the number 291023, the '3' is the least-significant digit, because if you change it you have the least effect on the overall number.

    To get the least significant byte, just bitwise-AND the larger int with 0xFF hex or 255 decimal (1+2+4+8+16+32+64+128=255)- that will clear out all the more-signficant bits and preserve the 8 least significant bits...

    int x = ...whatever...;
    unsigned char least_significant_bits = x & 255;
    

    The effect is like this:

    011101010101010101010101010110110 // x
    000000000000000000000000011111111  //255
    result:
    000000000000000000000000010110110 // x & 255
    

提交回复
热议问题