Difficulty: Beginner
Programming Language: C++

Article Index

Bitwise Operators

So far every operator we've seen has been performing an action on a value, which can be thought of as a way to interpret the binary data in the computer. The bitwise operators perform there actions directly on the underlying binary value, and we can modify each individual bit of a particular value.

We recommend going over the General Programming Tutorials covering binary to help fully understand bitwise operators.

Note: similar to the modulus operator, all bitwise operators can only be performed on integer value types.


Bitwise Not ~
~expression1;

Performs a not or flip on every bit in the provided operand and returns the result.

short var1 = 178; // Create variable `var1` and initialise to `178` (`00000000 10110010` in binary)

std::cout << ( ~var1 ) << "\n"; // Prints "-179" to the terminal    (`11111111 01001101` in binary)

Bitwise And &
expression1 & expression2;

Performs a bitwise and on every bit in the provided operands and returns the result.

short var1 = 178; // Create variable `var1` and initialise to `178`   (`00000000 10110010` in binary)
short var2 = 212; // Create variable `var1` and initialise to `212`   (`00000000 11010100` in binary)

std::cout << ( var1 & var2 ) << "\n"; // Prints "144" to the terminal (`00000000 10010000` in binary)

Bitwise Or |
expression1 | expression2;

Performs a bitwise or on every bit in the provided operands and returns the result.

short var1 = 178; // Create variable `var1` and initialise to `178`   (`00000000 10110010` in binary)
short var2 = 212; // Create variable `var1` and initialise to `212`   (`00000000 11010100` in binary)

std::cout << ( var1 & var2 ) << "\n"; // Prints "246" to the terminal (`00000000 11110110` in binary)

 

Left Bit Shift <<
expression1 << expression2;

Shifts all bits in expression1 to the left by the specified number of bits provided by expression2.

Take note that the bitshift operators are the same operators used by the input and output streams. The brackets here are essential to ensure that the operation is performed on the variables, and not on the stream. Without we'd see the output of `var1` immediately followed by the next parameter - in this we'd see "1781" and "1782", which is not what we want.

short var1 = 178; // Create variable `var1` and initialise to `178` (`00000000 10110010` in binary)

std::cout << ( var1 << 1 ) << "\n"; // Prints "356" to the terminal (`00000001 01100100` in binary)
std::cout << ( var1 << 2 ) << "\n"; // Prints "712" to the terminal (`00000010 11001000` in binary)
Right Bit Shift >>
expression1 >> expression2;

Shifts all bits in expression1 to the right by the specified number of bits provided by expression2.

short var1 = 178; // Create variable `var1` and initialise to `178` (`00000000 10110010` in binary)

std::cout << ( var1 >> 1 ) << "\n"; // Prints "89" to the terminal  (`00000000 01011001` in binary)
std::cout << ( var1 >> 2 ) << "\n"; // Prints "44" to the terminal  (`00000000 00101100` in binary)