在C/ c++ enum中," < "(雙尖括號)是什么意思?

[英]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. */

};

2 个解决方案

#1


9  

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

#2


5  

<< is a left bitshift operator.

<是一個左位移操作符。< p>

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 in decimal)這樣的位型,把它向左移動2,比如0010< 2,你會得到1000 (8 in decimal)。

An enum is simply an integer that is large enough to hold at least an int. Thus we can directly assign int values like 0, 1, etc. to it.

enum只是一個大到足以容納一個int的整數,因此我們可以直接指定int值,比如0、1等等。

In this case, we are assigning things like 1 << 8 to it (which yields 100000000 or 256 in decimal).

在這種情況下,我們將1 < 8賦值給它(它產生100000000或256的小數)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/06/15/6883c0d745fcc13b844669b1b43429e0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com