
Originally Posted by
Davidm44
Not sure about Why but I know BA is brilliant when it comes to this.
I like what you did there, shifting the letters around. Hehe~
Bitshifting has many purposes, although generally not needed when working with high-level code, sometimes you need to set\clear\get a flag.
Consider function one performs creation a new window. Do we want this window to have a frame? Should it be closable? What should it's ID be?
We have a WORD stored in memory(which is two bytes on a 32 bit system.). Consider the first byte will hold the window's ID, and the second byte will hold upto 8 flags. Note: This demonstrates bit-shifting, same can be done with using OR or XOR masks.
//Getting flags & ID
WORD buffer = 0; //Create a null dword
unsigned char bId = (char)buffer;
unsigned char bFlags = (char)buffer<<8;//Shift over 8 bits(one byte)
Setting Flags and ID
WORD buffer;
buffer |= (ID) ;
buffer |= (flags)>>8;
Or to set a given flag:
buffer |= 1<<(Offset in data holdiung flags);
Don't let the << confuse you with other classes, such as std::cout. These classes override the <<(bit-shit-left)operator to make it perform another task. By default the << bit-shifts left.