C++ SDL What does the | do?

后端 未结 4 2000
無奈伤痛
無奈伤痛 2021-01-24 13:37
screen = SDL_SetVideoMode(1000,1000,32, SDL_HWSURFACE | SDL_FULLSCREEN);

What does the | do in SDL_HWSURFACE | SDL_FULLSCREEN

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-24 14:08

    The | operator means bitwise or. That means each bit in the result is set if the corresponding bit in the left hand or the right hand value is set. So 1 | 2 = 3, because 1 in binary is 01 and 2 in binary is 10. So the result in binary is 11 which is 3 in decimal.

    In your example this is used to pass a lot of different on/off options to a function. Each of the constants has exactly one bit set. The function then looks at the value you pass and using the bitwise and operator & to check which ones you specified.

提交回复
热议问题