函数接受两个参数,一个字节和一个位字段,并返回字节中字段的值
我在网上找到了一些执行此任务的代码:
byte = byte >> field;
byte = byte & 0x01;
return(byte);
但是,我不明白为什么我们不能这样做:
return(byte & field);
这会起作用吗?为什么或为什么不呢?有更好的实现吗?
I found some code online that performs this task:
byte = byte >> field;
byte = byte & 0x01;
return(byte);
However, I don't understand why we can't just do this:
return(byte & field);
Will this work? Why or why not? Are there better implementations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这可以通过结构给出。可以说:
This can be given by a structure. Lets say:
第一个相当于:
它真正做的是转移到位置
field
的位,并返回1
如果该位被设置,0
否则。您提出的那个是不正确的,因为它没有移动到指定字段的偏移量。例如,
byte & 5 没有任何意义。
该函数也可以这样写:
当然,如果你打算传递
1 << 5
而不是5
,您可以按照自己的方式编写。我认为
field
是一个数字,指示我们感兴趣的位的位置,因此对于单个字节,它将在0..7
范围内。The first one is equivalent to:
What it really does is shift to the bit with position
field
and return1
if that bit is set,0
otherwise.The one you propose is incorrect because it doesn't shift to the offset of the designated field. For example,
byte & 5
doesn't make any sense.The function can as well be written like this:
Of course, if you intend to pass
1 << 5
to it instead of5
, you can write it your way.I suppose
field
is a number that indicates the position of the bit we are interested in, so for a single byte it would be in the range0..7
.在第一个代码示例中,field 是您想要其值的字段中的位的位置。
在第二个示例中,字段必须是该位设置为 1 的 int,即
1 <<字段
。In the first code sample, field is the position of the bit in the field of which you want the value.
In the second sample, field would have to be an int with that bit set to one, i.e.
1 << field
.如果您确实希望返回值为零或一,则如果您只关心返回值为零或非零,则可以
将
两种形式简化一点。
If you really want the return value to be zero or one you can either
or
Both forms simplify a bit if all you care about is that the return value is zero or non-zero.
右移字节
字段
表示右侧字的位数 (LSB)。字节=字节>> field
将带来 LSB 位置处的字节byte
的位数field
。然后byte = byte & 0x01
和带有0x01
的字节,这意味着如果在位号字段<中设置了位,则结果的 LSB 中将包含
1
/code> 最初,或者如果在该位置清除了该位,则将包含0
。例如,需要检查字节
0x52
是否已设置其位数4
的测试如下所示。但我们不能做
byte & field
检查编号为field
的位是否被设置或清除。这是因为field
只是一个二进制文件,而不是一个掩码。如果我们这样做byte &字段
那么就会发生以下情况。字段
的值为0x04
,即设置的位数2
。通过此操作,我们实际上检查了位号2
是否已设置。如果field
的值为5
,那么位0
和2
将被设置,所以像上面一样直接进行AND运算如果提取byte
值中的位0
和2
的状态,则会产生四种可能的组合。将 0x01 向左移位并制作掩码
测试
byte
的位值的其他方法是不是移位byte
本身,而是移位0x01
将field
向左掩码一次,并将其与字节进行 AND,并检查是否为零。当field
编号位被设置时,(byte & (0x01 << field)) != 0
将为 true,否则为 false。使用预计算掩码
如果您有一个需要定期测试的特定格式的字节,例如某些预定义字的某些位字段,其中每个位都表示一些特定的事物,那么您可以保留预计算掩码,它仅在特定的情况下有一个将使用该掩码测试的位位置。例如,要检查一个字节,预计算机掩码将是:
因此,要测试
byte
中的位 4,我们必须执行return (byte & BIT_4)
或return (byte & BIT_4) != 0;
根据位位置表示的宏名称可以设置。
Shift byte to right
field
represents the bit number of the word you have from the right hand side (LSB).byte = byte >> field
will bring the bit numberfield
of the bytebyte
at the LSB position. Thenbyte = byte & 0x01
ands the byte with0x01
that means the result will contain a1
in the LSB if it had the bit set in the bit numberfield
originally, or will contain a0
if it had the bit cleared at that position.For example the test needed to check if the byte
0x52
has its bit number4
set or not is shown below.But we cannot do
byte & field
to check if the bit which is numberedfield
is set or cleared. This is becausefield
is simply a binary and not a mask. If we didbyte & field
then the following would happen.The
field
has the value0x04
which is, its bit number2
set. With this operation we have checked actually if the bit number2
is set. If the value offield
was5
then bit0
, and2
would be set, so ANDing directly like above would result if extraction of the state of bit0
and2
in the value ofbyte
, which can take four possible combination.Shift 0x01 to left and make mask
Other ways to test the bit value of a
byte
is instead of shifting thebyte
itself, we shift the0x01
maskfield
times to the left, and AND it with the byte, and check if is zero or not.(byte & (0x01 << field)) != 0
will be true when thefield
numbered bit is set or false otherwise.Use precomputed mask
If you have a byte with certain format which you need to test regularly, for example some bitfield of some pre defined word, where each bit means some specific thing then you can keep precomputer mask, which has a one only in the specific bit position which will be tested with that mask. For example to check for one byte the precomputer masks would be:
So to test the bit 4 in
byte
we have to doreturn (byte & BIT_4)
orreturn (byte & BIT_4) != 0;
Depending on what the bit position represents the name of the macro could be set.
第一个示例将所需的位移动到返回的 LSB 中,但第二个示例只是屏蔽掉所有不需要的位。
The 1st example moves the required bit into the LSB of the return but the second example simply masks out all the bits that are not required.