Bitwise Operators in C++
A while ago I was given some directed reading on bitwise operators in C++. I've been so busy with coursework, I haven't had time to take a look! Thankfully, I found some time and thought that it would make a good post here.
Bitwise operators in C++ appear to be very similar to those in Javascript. They operate on things at a binary level - i.e. operating on the individual bits that make up a number. Apparently there are 6 different operators:
&
- Bitwise AND|
- Bitwise OR^
- Bitwise XOR (eXclusive OR)~
- Bit inversion (monadic)<<
- Shift bits to the left>>
- Shift bits to the right
I'll explain each of these with examples below.
Bitwise AND
Bitwise AND takes a bit from each thing (usually a number), and outputs a 1 if they are both 1s, and a 0 otherwise. Here's an example:
87654321
--------
01011010 // 90
11010101 // 213
-- AND --
01010000 // 80
In the above example, the only 1s that actually make it through to the final result are positions 7 and 5, which are worth 64 and 16 respectively. This can be useful for extracting a specific bit from a number, like this:
#include <iostream>
using namespace std;
void main() {
int c = 58, d = 15;
cout << "c " << (c & 32) << endl;
cout << "d " << (d & 32) << endl;
}
In the above, I create 2 variables to hold the 2 numbers that I want to test, and then I perform an AND on each one in turn, writing the result to the console. It should output something like this:
c 32
d 0
This is because 58 is 00111010
, and 32 is 00100000
, so only the 6th bit has a chance of making ti through to the final result.
Bitwise OR
Bitwise OR outputs a 1, so long as any of the inputs are true. Here's an example:
87654321
--------
10110101
00011101
-- OR --
10111101
Bitwise XOR
Bitwise XOR stands for exclusive OR, and outputs a 1 so long as either of the inputs are 1, but not both. For example, 1 ^ 0 = 1
, but 1 ^ 1 = 0
.
87654321
--------
10101101
11001110
-- XOR --
01100011
Bit inversion
Bitwise inversion is a monadic operator (i.e. it only takes one operand), and flips every bit of the input. For example 11011101
(221) would become 00100010
(34), although this greatly depends of the type and length of the number you are using.
Bit shifting
Bitshifting is the process of shifting a bit of bits to the left or right a given number of places. For example, if you shift 1010
(10) over to the left 1 place you get 10100
(20), and if you shift 0111
(7) over to the right by one place you get 0011
(3). If you play around with this, you notice that this has the effect of doubling and halving the number:
cout << "f ";
int f = 5;
for(int i = 0; i < 5; i++)
{
f = f << 1;
cout << f << " ";
}
cout << endl;
cout << "g ";
int g = 341;
for(int i = 0; i < 5; i++)
{
g = g >> 1;
cout << g << " ";
}
cout << g << endl;
It doesn't deal with the decimal place, though - for that you need something like a float
or a double
. Here's some example output from the above:
f 10 20 40 80 160
g 170 85 42 21 10 10
At first glance, the bit shifting operators in c++ look the same as the ones used to stream things to and from an input / output stream - I'll have to be careful when using these to make sure that I don't accidentally stream something when I meant to bitshift it.
Bitshifting can be useful when working with coloured pixels. You can set a colour like this:
unsigned int newCol = ((unsigned int) 248)<<16) +
((unsigned int) 0)<<8) +
((unsigned int) 78);
I think that just about covers bitwise operators in C++. If you're interested, you can find the source code that I've written whilst writing this post here (Raw).