C++ Fundamentals: Understanding the Basics
Bitwise Operators In C++
Introduction
In this post, we'll look at C++'s bitwise operators using examples. Bitwise operators in C++ operate on integer data at the bit-level level. The real bits may be tested, set, or shifted as part of these operations. The six bitwise operations that are part of C++ are listed below.
Different kinds of bitwise operators that operate on integers at the bit level are supported by C++. Bitwise operators that are supported include:
- & Bitwise AND
- | Bitwise OR
- << Bitwise Left Shift
- >> Bitwise Right Shift
- ~ Bitwise Complement
- ^ Bitwise XOR
& [Bitwise AND]
Returns 1 if both the bits are 1 (1, 1). Else ((1, 0), (0, 1), (0, 0)), it returns 0.
Eg.
4 = 100 (base-2 binary system)
5 = 101 (base-2 binary system)
100
101
___
100, (equates to 4 in binary system)
| Bitwise OR
1 if at least one of the bits is 1.
0 if both of them are 0.
Eg.
4 = 100 (base-2 binary system)
5 = 101 (base-2 binary system)
100
101
___
101, which is equal to 5 in the base-10 decimal system.
<< Bitwise Left Shift
The left shift operator shifts all the bits towards the left side according to the number of bits specified and adds trailing 0’s to the right side.
Eg.
4 = 100 (base-2 binary system)
4 << 1
100 << 1
100 + 0
1000 which is equal to 8 in the base-10 decimal system
4 << 2
100 << 2
100 + 00
10000 (equates to 16 in decimal)
>> Bitwise Right Shift
The right shift operator shifts all the bits towards the right side by the number of bits specified and discards the rightmost bits.
Eg.
4 = 100 (base-2 binary system)
4 >> 1
0 + 100 - 0
In the example above, 0 is added to the left side of 4 (100 in base-2). Then, the rightmost bit is removed, which yields 10 (base-2) or 2 (base-10).
4 << 2
00 + 100 + 00
In the example above, 00 is added to the left side of 4 (100 in base-2). Then, the two rightmost bits are removed, which yields 1 (base-2) or 1 (base-10).
~ Bitwise Complement
The complement operator flips the binary digits, that is from 0 to 1 and from 1 to 0.
Eg.
5 = 0000000000000101 in base-2 binary system
~5 = 1111111111111010 in base-2 binary system
=> -6 in base-10 binary system.
unsigned(5) = 00000000000000000000000000000101 in base-2 binary system
~5 = 11111111111111111111111111111010 in base-2 binary system
=> 4294967290 in base-10 binary system
The first bit of a signed integer is used to determine whether the result is positive or negative. Therefore, a signed 5 and an unsigned 5 yield different results.
^ Bitwise XOR
The exclusive or (XOR) operator returns 1 if both the operands are different and returns 0 if both of them are the same (1, 1 or 0, 0)
12 = 1100 (base-2 binary system)
10 = 1010 (base-2 binary system)
1100
1010
____
0110 which is a 6 in binary system.