Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "<<" (double angle brackets) mean in C/C++ enum?

Tags:

c++

c

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. */

};
like image 320
user1459427 Avatar asked Dec 07 '22 14:12

user1459427


1 Answers

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

(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
like image 166
Ryan Avatar answered Dec 09 '22 13:12

Ryan