简体   繁体   English

“<<”(双尖括号)在C / C ++枚举中意味着什么?

[英]What does “<<” (double angle brackets) mean in C/C++ enum?

enum ofp10_port_state {

    OFPPS10_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
    OFPPS10_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
    OFPPS10_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
    OFPPS10_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
    OFPPS10_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */

};

Its a left bit shift operator. 它是一个左移位运算符。 Meaning it shifts the bits left the indicated number of bits: 这意味着它将位移位指定的位数:

say that the value is: 说价值是:

0x0F or 00001111
0x0F << 4 = 0xF0 or 11110000

In microsoft c++ shifts right (>>) keep the sign (or the most significant digit, the one on the far left) depending on if the number is signed or unsigned 在microsoft c ++中右移(>>)保留符号(或最高位数,最左边的数字),具体取决于数字是有符号还是无符号

(assuming size of a byte): (假设一个字节的大小):

signed integer (an int for example):
0x80 or 10000000
0x80 >> 7 = 11111111
0x10 or 00010000
0x10 >> 4 = 00000001
if its unsigned (a uint):
0x80 or 10000000
0x80 >> 7 = 00000001
0x10 or 00010000
0x10 >> 4 = 00000001

<< is a left bitshift operator. <<是左移位运算符。

If you have a bit pattern like 0010 (2 in decimal) and shift it to the left by 2 like so 0010<<2 , you get 1000 (8 in decimal). 如果你有一个比特模式,如0010 (十进制2),并将它向左移动2,像0010<<2 ,你得到1000 (十进制8)。

An enum is simply an integer that is large enough to hold at least an int . 枚举只是一个足以容纳至少int Thus we can directly assign int values like 0, 1, etc. to it. 因此,我们可以直接为其分配0,1等int值。

In this case, we are assigning things like 1 << 8 to it (which yields 100000000 or 256 in decimal). 在这种情况下,我们将1 << 8分配给它(它产生100000000或十进制的256)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM