Whether we like it or not, our society is money-oriented. Earning money takes time, and time has its limits; none of us has more of it. So if you want to maximize what you're earning, you must improve your speed. Bitwise operators may not always be the best choice for this, but in some cases relying on them will pay off quite handsomely.

Bitwise Operators in Action - How To (Page 3 of 4 )

As presented before, bit fields can save memory quite well if the structure has many instances. A place where this is used is by representing the RGB color scheme. If you aren’t in the domain of IT from yesterday, you probably know that for monitors, the color is made up with the combination of three colors: red, green, and blue. Each color is a combination of these three. You may also know that each color can have a quantity from 0 to 255. Let me present how is this efficiently saved in memory:

AAAA AAAARRRR RRRRGGGG GGGGBBBB BBBB

And all this requires 4*8 = 32 bits that are equal to 4 bytes or a double word. The first byte (from the right) is named the low byte while the last is the high byte. On the low byte is saved the blue color; going to the left, the next ones in order are the green and the red, while on the high byte, the alpha represents the color's transparency. Of course, this represents the 32-bit color palette, but the same is true for the 16-bit version; you just need to throw away the alpha bytes and three bytes from the other colors.

Extracting a color value from the upper example is quite easy once you recall what you learned in the first part of the article. We just need to choose a good mask to use that throws away the rest of the bits. The correct bitwise operator is the end, as follows:

AAAA AAAARRRR RRRRGGGG GGGGBBBB BBBB

& 0000 0000 0000 0000 1111 1111 0000 0000

And we’ll have only what we need:

0000 0000 0000 0000 GGGG GGGG 0000 0000

Shift it with 8 bits to the right and you’ll have the value in a DWORD type. You don’t have to write down the upper long binary number; just pack each 4 digits into a hex number and add the OX in front of it. So the upper mask will look like this: 0X0000FF00. For each color, a different mask will be used. With the “or” bitwise operator, the modification can be resolved just as easily. Take a second to think about how it can be done.

Now if you look into some programming languages where a graphic user interface can be drawn, you probably observed that for styles, the definitions are “or” with bitwise operators. This is because the definitions are made in such way that only a bit is turned on inside, so when you “or” them in the result, for every option its one bit will be turned on. Look at the example below to understand it better (please note that of course longer bits are used, but for simplicity 4 will be enough for us).

#define OPTION_1 1 // in binary system: 0001

#define OPTION_2 2 // in binary system: 0010

#define OPTION_3 4 // in binary system: 0100

#define OPTION_4 8 // in binary system: 1000

So later on, if we bitwise or OPTION_1 and OPTION_2 we’ll get 0101. That is 5. Now all we need to do is ask each bit if it is on or not (use the “and” operator again) and treat it. It's a clever move, isn’t it? And all of this is really memory friendly.

Another practical example for everyday programming with integers is the following: how do you swap two values without any extra type present? The solution is in the XOR operator, just as presented below. Let “a” and “b” contain a Value_1 and Value_2, and also observe that any value for which you apply XOR on itself becomes 0, and any value for which you apply the XOR operator with the 0 value will remain the same value.

Operation:

a = a ^ b; // a = Value_1 ^ Value_2 , b = Value_2

b = a ^ b; // a = Value_1 ^ Value_2,

//b = Value_1 ^ Value_2 ^ Value_2 = Value_1

a = a ^ b; // a = Value_1 ^ Value_2 ^ Value_1 = Value_2

And it will be blazing fast also. Two birds killed with just one stone.