Bit flags and bitwise manipulation

learn how manage large boolean data using bitwise manipulation and bit flags

Have you ever wondered what does this mean?

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT DEPTH);

glut, as OpenGL or many other frameworks, libraries and utilities has tons of boolean values that has to be setted in order to achieve a correct initialization, that can seem easy but it’s not.

C++, as many other languages such Javascript, Java or Python need variables to have a minimal size, usually the “word size”, this is the minimal bit block needed to store a “word” , known as minimal addressable storage unit.
This usually means that the minimal addressable unit has 1 byte length, from booleans to strings, everything has to be greater than 1 byte length (8 bits).

Where’s the problem?
Easy, a boolean variable can have 2 values (0 or 1), so just 1 bit is needed, but it takes 8 bits to store it, so, if we have 100 boolean variables in our program we will need 100 bytes to store them (800 bits).
Wouldn’t be awesome to use all the bits in that byte to store 8 booleans in the space that just 1 would require?
Continue reading “Bit flags and bitwise manipulation”

Design a site like this with WordPress.com
Get started