C语言 AND 0xFF 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14713102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does AND 0xFF do?
提问by Muis
In the following code:
在以下代码中:
short = ((byte2 << 8) | (byte1 & 0xFF))
What is the purpose of &0xFF?
Because other somestimes I see it written as:
的目的是&0xFF什么?因为有时我看到它写成:
short = ((byte2 << 8) | byte1)
And that seems to work fine too?
这似乎也能正常工作?
采纳答案by John Colanduoni
Anding an integer with 0xFFleaves 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 byte1is 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.
Anding 一个整数,0xFF只留下最低有效字节。例如,要获取 a 中的第一个字节short s,您可以编写s & 0xFF. 这通常称为“掩蔽”。如果byte1是单字节类型(如uint8_t)或已经小于 256(因此除最低有效字节外全为零),则无需屏蔽高位,因为它们已经为零。
See tristopiaPatrick Schlüter's answer below when you may be working with signed types. When doing bitwise operations, I recommend working only with unsigned types.
当您可能使用签名类型时,请参阅下面的 tristopiaPatrick Schlüter 的回答。在进行按位运算时,我建议只使用无符号类型。
回答by D Stanley
if byte1is an 8-bit integer type then it's pointless - if it is more than 8 bits it will essentially give you the last 8 bits of the value:
如果byte1是 8 位整数类型,那么它毫无意义——如果它超过 8 位,它基本上会给你值的最后 8 位:
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
& 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
-------------------------------
0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1
回答by Patrick Schlüter
The danger of the second expression comes if the type of byte1is char. In that case, some implementations can have it signed char, which will result in sign extension when evaluating.
如果 的类型byte1是,则第二个表达式的危险就来了char。在这种情况下,某些实现可以拥有它signed char,这将在评估时导致符号扩展。
signed char byte1 = 0x80;
signed char byte2 = 0x10;
unsigned short value1 = ((byte2 << 8) | (byte1 & 0xFF));
unsigned short value2 = ((byte2 << 8) | byte1);
printf("value1=%hu %hx\n", value1, value1);
printf("value2=%hu %hx\n", value2, value2);
will print
将打印
value1=4224 1080 right
value2=65408 ff80 wrong!!
I tried it on gcc v3.4.6 on Solaris SPARC 64 bit and the result is the same with byte1and byte2declared as char.
我试了一下GCC v3.4.6在Solaris SPARC 64位,结果是相同的byte1,并byte2声明char。
TL;DR
TL; 博士
The masking is to avoid implicit sign extension.
屏蔽是为了避免隐式符号扩展。
EDIT: I checked, it's the same behaviour in C++.
编辑:我检查过,它在 C++ 中是相同的行为。
回答by sr01853
Assuming your byte1is a byte(8bits), When you do a bitwise AND of a byte with 0xFF, you are getting the same byte.
假设您byte1是一个字节(8 位),当您对一个字节与 0xFF 进行按位与运算时,您将获得相同的字节。
So byte1is the same as byte1 & 0xFF
所以byte1是一样的byte1 & 0xFF
Say byte1is 01001101, then byte1 & 0xFF = 01001101 & 11111111 = 01001101 = byte1
说byte1是01001101,那么byte1 & 0xFF = 01001101 & 11111111 = 01001101 = byte1
If byte1 is of some other type say integer of 4 bytes, bitwise AND with 0xFF leaves you with least significant byte(8 bits) of the byte1.
如果 byte1 是其他类型的 4 字节整数,则按位 AND 与 0xFF 会为您留下 byte1 的最低有效字节(8 位)。
回答by Jerry Coffin
The byte1 & 0xffensures that only the 8 least significant bits of byte1can be non-zero.
的byte1 & 0xff确保只有8的至少显著位byte1可以是非零的。
if byte1is already an unsigned type that has only 8 bits (e.g., charin some cases, or unsigned charin most) it won't make any difference/is completely unnecessary.
如果byte1已经是只有 8 位的无符号类型(例如,char在某些情况下,或unsigned char在大多数情况下),它不会有任何区别/完全没有必要。
If byte1is a type that's signed or has more than 8 bits (e.g., short, int, long), and any of the bits except the 8 least significant is set, then there will be a difference (i.e., it'll zero those upper bits before oring with the other variable, so this operand of the oraffects only the 8 least significant bits of the result).
如果byte1是有符号类型或具有超过 8 位的类型(例如,short, int, long),并且设置了除 8 个最低有效位之外的任何位,则将存在差异(即,它将在oring之前将那些高位归零与另一个变量,所以这个操作数or只影响结果的 8 个最低有效位)。
回答by thumbmunkeys
it clears the all the bits that are not in the first byte
它清除所有不在第一个字节中的位
回答by Alexey Frunze
& 0xFFby itself only ensures that if bytes are longer than 8 bits (allowed by the language standard), the rest are ignored.
& 0xFF本身仅确保如果字节长于 8 位(语言标准允许),则其余部分将被忽略。
And that seems to work fine too?
这似乎也能正常工作?
If the result ends up greater than SHRT_MAX, you get undefined behavior. In that respect both will work equally poorly.
如果结果最终大于SHRT_MAX,则会出现未定义的行为。在这方面,两者的效果都一样差。

