14

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 :(

Anthon
  • 69,918
  • 32
  • 186
  • 246
Pangu
  • 3,721
  • 11
  • 53
  • 120
  • 1
    I edited your code as I think there were two errors ('less significant byte' and 'void riteThi', I also updated the formatting of code. You can roll that back if you find it inappropriate. BTW isn't there an `a` missing in 'Ieysu'? – Anthon May 14 '13 at 05:22
  • first off, thanks for editing my code...secondly the "rite" was done intentionally (i.e. for a class ;)...thirdly HAHA! yes there is suppose to be an a :)..long story about it that I wont get into – Pangu May 14 '13 at 08:34

4 Answers4

28

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 decimal digit, because if you change it you have the least impact on the overall number. In binary, we deal in binary digits (abbreviated to "bits"), and a byte is defined as 8 bits, but the concept holds.

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;

(You could also store the result in a wider type, like int, or a std::byte or std::uint8_t).

The bitwise-AND operation works like this, producing 1s only when both inputs are 1:

011101010101010101010101010110110 // x
000000000000000000000000011111111  //255
result:
000000000000000000000000010110110 // x & 255
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
12

Your int consist of several bytes (most likely 2, 4 or 8 bytes). Similar to the concept of the least significant bit, the least significant byte is the byte that has the less weight in the value of the entire integer. Depending on the endianness of your system, it may be the first or last byte in terms of memory.

To extract the least significant byte, bitwise-AND the number with one byte worth of 1s, i. e. 255:

int LSB = (someInteger & 0xFF);
5

The more significant bits of an int are the ones that contribute more to its value. The least significant bits contribute less. You can normally obtain these by anding the integer with 255 (a byte with all 1s)

int lsb;
lsb = SumInt & 255;
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • if you & it, dont you end with lsb = SumInt again? – Pangu May 14 '13 at 05:08
  • 1
    Only if the value of SumInt is smaller or equal 255 and bigger than -1 (assuming that the most significant bit indicates the sign as on most modern processors). – Anthon May 14 '13 at 05:16
5
  • When you look at a decimal number, say 507, the least significant digit would be the 7. Changing it to a 6 or an 8 would change the overall number a lot less than changing the 5.

  • When you look at a date, say May 14, 2013, the least significant part is the day (14) in terms of chronological ordering.

  • When you look at an unsigned 32-bit int (4 bytes), where the value of the integer is (256^3)*b3 + (256^2)*b2 + 256*b1 + b0 for the 4 bytes b0, b1, b2, b3, the least significant byte is the byte b0.

You can get the least-significant byte from your int sumInt by doing char c = sumInt & 0xFF; as others have suggested.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173