问题描述
在以下code:
short = ((byte2 << 8) | (byte1 & 0xFF))
什么是&放大器的目的;为0xFF
?
因为其他somestimes我把它写成:
What is the purpose of &0xFF
?Because other somestimes I see it written as:
short = ((byte2 << 8) | byte1)
这似乎很好地工作呢?
And that seems to work fine too?
推荐答案
与安定 0xFF的
只留下至少显著字节整数。例如,为了获得在短S中的第一个字节
,你可以写 S&安培; 0xFF的
。这通常被称为掩蔽。如果字节1
是一个单字节类型(如 uint8_t有
)或已小于256(,因此是除了至少显著字节全零)没有必要掩盖了更高位,如他们已经为零。
Anding an integer with 0xFF
leaves only the least significant byte. For example, to get the first byte in a short s
, you can write s & 0xFF
. This is typically referred to as "masking". If byte1
is either a single byte type (like uint8_t
) or is already less than 256 (and as a result is all zeroes except for the least significant byte) there is no need to mask out the higher bits, as they are already zero.
请参阅 tristopia 行使>帕特里克SCHLÜTER的答案时,你可能会签订各类合作下方。在做位操作,我建议只与无符号类型的工作。
See Patrick Schlüter's answer below when you may be working with signed types. When doing bitwise operations, I recommend working only with unsigned types.
这篇关于什么是到0xFF吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!