BitWise operator explanation?

Ok, so I'm getting into game development, and (despite what I said earlier) I'm using C++. I'm going to be using the amazing and incredible SFML library. I was watching CodingMadeEasy's tutorials on it, and he did the following code(I commented what I understand and don't understand):

sf::RenderWindow window; // 1

window.create( sf::VideoMode(800,600), "My First SFML Game", sf::Style::TitleBar | sf::Style::Close ); // 2



This creates a new instance of the RenderWindow class found in the sf namespace called "window". Pretty simple stuff!


I understand most of this code, but this is what I made this topic for. I understand it up until the | bitwise operator. It creates a window with a size of 800x600 and the title of it is "My First SFML Game". Then we style the window so it has a close button, but hold up a second! Why do we do sf::Style::TitleBar | sf::Style::Close??? Why do we need the | symbol?? I don't understand!

Also, sorry for the extremely wide code section :( I can't fix that!


The comments in the code basically asked the question for me! So yeah! Please help! :)
Last edited on
| is a binary OR operator. I explain it in detail here:

Basically, each of those "TitleBar" and "Close" values have 1 bit set, and the | combines them.
...sorry for the extremely wide code section...

You could always hit the carriage return/enter key...

Say you have two bits x and y. Bits are individual parts of a byte and can only hold two states: on or off (1 or 0 respectively).

The OR bitwise operator returns 1 if either x or y is 1.

In your case, the OR bitwise operator is used to flip on bitflags. Bitflags are like using an array or container of bools (though there is the <bitset> library that does the same).

Let's say the bytes of TitleBar and Close look lik 00010000 and 00000010 respectively. So, ORing them would look like:
0 0 0 1 0 0 0 0
| 0 0 0 0 0 0 1 0
0 0 0 1 0 0 1 0
The result is two bitflags switched on, so the internal code in sf::RenderWindow will know to include both a titlebar and a close button.

Some links for information on other bitwise operators:
(found this one to be the most helpful for me)

A table you can use to help you remember what each operator does (I use it all the time):

    operator &
        x & 0 = 0
        x & 1 = x
    operator |
        x | 0 = x
        x | 1 = 1
    operator ~
        ~0 = 1
        ~1 = 0
    operator ^
        x ^ 0 = x
        x ^ 1 = ~x
    operator <<
        00000001 << 1 = 00000010
    operator >>
        00000010 >> 1 = 00000001
Last edited on
Thanks everyone!

Also, @Daleth:

It folded in the writing part... sorry!
After a bit of googling, I found this site:

The bitwise operator '|' returns true if either argument provided is true.

After further search, I found the declaration for the namespace sf::style

And found that 'TitleBar', 'Close', and others are actually numbers. So sf::style::TitleBar | sf::Style::Close is the same as 1 | 4. This returns the value 5 so this since there is no value 5 in the sf::Style namespace, this will then correspond to sf::Style::Default

The default style, which is a shortcut for Titlebar | Resize | Close

If you were not able to follow my rambling, I provided some links. Gl
Topic archived. No new replies allowed.