Bitwise Operators

In this blog i will talk about some bitwise operators and their uses in programming. Operators such as: << , >> , | , & , ^ , ~ will be discussed.

The bitwise left shift operator <<

The bitwise shift operators works by shifting a number by a number of bits. For example if you shift the int value of 5 by the number of bits 2 in binary form, so 0100 now becomes 10100. We essential shifted the binary number 0100 by two bits. An interesting application for this is

that when you shift 5 by 2 bits you are multiply 5 by 2^2 (2 to the power of 2) which is 5 * 4 which is 20. In the watch window in visual studio i tracked the variable leftShift executed that line of code in the debugger. It shows 10100 in binary, i also converted the integer value of 20 in binary form. They have the same binary value. Left shifting 5 by 2 gives you 20.

LeftShift

The bitwise right shift operator >>

Similar to the left shift operator the right shift shift the bits to the right. Similar instead of multiplying the original number you divide the number by the bits shifted by the power of 2. For example 128 shifted right by 5 divides 128 by 2^5 which is 16.

RightShift

The bitwise AND operator &

The AND operator compares each bit in the binary format of an integer. If the comparison has both bits set to 1 it returns a 1 otherwise it is a 0. A handy application for this is checking to see if a number is odd or even. If you use the AND operator to check the right most bit against 1 you can determine if the number is odd or even.

AND

The bitwise OR operator |

The OR operators compares each bit in each integer. If the comparison has either 1 the results is 1 or if the compares has each bit as 0, the result is 0. So for example 01011 | 10011 is 11011. A good use of the OR operator and also the AND operator is that you can pass in multiple options into a single integer value. This is often used in OpenGL in the function glClear seen below:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Two options have been passed into a single GLbitfield which is a unsigned int.

OR

The constants that are defined at the top of the program are in the power of 2’s. This is the same in OpenGL where the attribute masks are defined in GL.h

The bitwise NOT operator ~

The NOT operator flips each bit in the binary sequace. Bits that are 0 become 1 and bits that are 1 become 0. So 01010 is then 10101.

NOT

The bitwise XOR operator ^

The XOR operator compares each corresponding bit in a binary format of a number. If the comparison has each bit as 1 and each bit as 0, the result is 0. If the comparison has one bit as 0 and the other 1 the the result is 1. So for example 1010 XOR 0101 is now 1111.

NOT

Note on visual studio:

To bring up the watch window in visual studio go to “Debug” -> “Windows” ->”Watch”. Make sure you are in debug mode first.

Posted in C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s