Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does least significant byte mean?

Tags:

c++

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 buffer used to store a byte of data, let's call it:

char &bitter;

In my header file, I have this:

void writeThisByte(int sumInt);

The instructions says that I have to write the "less significant byte" of the int being passed in to an ostream object, i.e. strobj;

However, I am confused on exactly what the least significant byte means.

Does this mean you are checking to see whether sumInt == 1? And if it is, do you write it to the ostream like this?

strobj.write(&bitter, 1);

I'm not sure :(

like image 428
Pangu Avatar asked May 14 '13 04:05

Pangu


People also ask

What is the least significant byte?

The "least significant" byte is the one for the smallest powers of two: 27, ..., 20. For example, say that the 32-bit pattern 0x12345678 is stored at address 0x00400000. The most significant byte is 0x12; the least significant is 0x78.

What do you mean by least significant bit?

In a binary number, the bit furthest to the left is called the most significant bit (msb) and the bit furthest to the right is called the least significant bit (lsb).

What is significant byte?

The most significant byte is the byte in a multiple-byte word with the largest value. As with bits, the most significant byte is normally the byte farthest to the left or the byte transmitted first in a sequence.


1 Answers

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
like image 59
Tony Delroy Avatar answered Oct 11 '22 12:10

Tony Delroy