问题描述
我必须实现一个方法写一个字节到 ostream
对象。让我们调用这个 ostream
object strobj
。我也有一个位缓冲区用于存储一个字节的数据,我们称之为:
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);
指令说,我必须写入int的较低有效字节 ostream
对象,即 strobj
;
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.
这是否意味着你正在检查 sumInt == 1
?如果是这样,你能像这样写入 ostream
吗?
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);
我不确定:(
推荐答案
想象一个32位整数,其中每个位可以是0或1,例如:
Imagine a 32-bit integer, where each bit can be 0 or 1, say:
011101010101010101010101010110110
^^^^^^^^
最低有效字节
这可能更容易理解,如果你采取一个十进制的例子:给定数字291023,'3'是最小的 -
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.
为了得到最低有效字节,只需按位AND就可以将较大的 int
使用0xFF十六进制或255十进制(1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255) - 将清除所有更重要的位, 8个最低有效位...
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;
效果如下:
011101010101010101010101010110110 // x
000000000000000000000000011111111 //255
result:
000000000000000000000000010110110 // x & 255
这篇关于最低有效字节是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!