What does least significant byte mean?

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

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

回答1:

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 


回答2:

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); 


回答3:

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; 


回答4:

  • 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!