I have a basic idea of using them and how to do so (very very basic idea).
But what I can't get is what is actually the point of using them?
The only one I have seen being used is | operator, in setting the flags for an fstream and sometimes in WinAPI functions.
But I couldn't find anything definite on how they work?
I would really appreciate it if some one would explain those to me!
But how is that code you gave working? (that is what is mystifying me more than using them).
Well, for bitwise operatios (duh).
Funny!
So, what is the point of bitwise operations?(That is what I couldn't think of, no examples of any actual operation that couldn't be performed without them)
You have to think in binary when using bitwise operators. Key attack equals 00010000 in binary and KEY_LEFT equals 00000100. When you | (or) a value then you compare each position of both numbers and if either is a one in that position it returns 1 otherwise it returns 0 (so each bit can function as a flag).
So:
00010000
00000100
-------------
00010100 after the OR operation. keyState is set to equal this so when you &(and) the
numbers you are left with that individual flag. So once again we & keyState with KEY_LEFT to get
00010100
00000100
-------------
00000100 which is the value of KEY_LEFT. Since a positive value is converted to true in bool
then the if statements evaluate to true. If that flag wouldn't have been set it would come back 0 which evaluates to false in bool.
Does that make sense?
Bitwise operations allow you to use one block of memory to store many possibilities with flags. Instead of having to have a variable for each possibility you can use one which is much more efficient and saves memory (which isn't near the problem now as it used to be, but in larger programs in could come to that point).