They are often used as "flags"; take a char, for example. Eight bits, typically.
00000000
I'm going to use this to carry information in my code. If the first bit is a zero, it means we're in PLAYBACK_MODE. If the first bit is a one, we're in RECORD_MODE.
If the second bit is one, we're operating at 50_FRAMES_PER_SECOND. If the second bit is zero, we're on 25_FRAMES PER SECOND.
So 11000000 means record at 50 frames per second.
10000000 means record at 25 frames per second.
I set these individual bits with bitwise operators, and pass the value as a single char parameter. Maybe I'm really tight on memory, maybe I don't want to have to pass across eight boolean values to every function.
Often, it will be done using defined values in advance for the flags, so the code ends up looking like:
1 2
|
char mode = ( 50_FRAMES_PER_SECOND | RECORD_MODE);
setNewModeOnHardware(mode);
|
So that's one use.
-----------------------
Another use is the wide world of "bit twiddling". Modern compilers can do a lot of this for us, but more primitive compilers won't, and I suspect some of the bit twiddles won't be spotted by even modern compilers.
For example, how do you turn a (fairly standard) floating point number into its own negative? You flip the first bit. How do you flip single bits? Bitwise operators. I'd expect a modern compiler to spot that one for you, but that's just an example. Here are many more;
https://graphics.stanford.edu/~seander/bithacks.html