Javatpoint Logo
Javatpoint Logo

Bitwise Operators in C++

Bits can be managed in a variety of ways using bitwise operators. The way we utilise bitwise operators like (|, &,,,>>, ) among bits and mathematical operators like (+, -, /, *) among integers are equivalent.

In this article, we will examine six different categories of bitwise operators in C++. In C++, we will comprehend both their internal workings and their syntax.

  • AND (&)
  • OR (|)
  • XOR (^)
  • COMPLEMENT (~)
  • Left Shift (<<)
  • Right Shift (>>)

While some of these operators' names and symbols may resemble those of logical operators, they are not interchangeable.

Bitwise Operators vs Logical Operators in C++:

For beginners, bitwise operators like AND, OR, and XOR can be confusing.

Logical AND and Logical OR may be familiar to you if you have previously studied logical operators. According to many individuals, they frequently get mixed up with the Bitwise AND and Bitwise OR operators. Let's try to figure out how they differ from one another. The logical operators use Boolean data and output a True or False Boolean value.

The bitwise operators in C++ operate on integers; i.e., they accept integer inputs, do bit manipulation, and then return an integer output. While the logical AND and OR use '&&' and '||' as their operators, the bitwise AND and OR use '&' and '|'.

Bitwise operator types in C++

Now that you know the distinction between logical and bitwise operators and what bitwise operators are, let's take a closer look at each of them.

AND (&) Operator:

A single & symbol is used to represent the bitwise AND operator. This binary operator requires two operands, or two numbers, in order to function.

It conducts a bit-level logical AND operator on the binary values of the left and right operands, i.e., if both operands have a 1 in the desired place, the output will also have a 1 in the same position, else it will be 0.

Truth table for Bitwise AND operator in C++:

Num1 Num2 Result=Num1 & Num2
0 0 0
1 0 0
0 1 0
1 1 1

Example:

Output:

3 & 4 = 0

Working Mechanism:

The bitwise AND operation on 3 and 4 is carried out by the snippet of code above. Let's take a closer look at how they operate.

  1. In binary, 4 equals 100 and 3 is 11
  2. By adding zeros to the left side of the most significant bit, the shortest binary value must first be converted to the length of the longest binary value.
  3. Here, 3 has a length of 2, whereas 4 has a length of 3, making it the greatest number. Add 0s as the most significant bit in 3 to make them the same length.
  4. The binary representation for 3 is now 011, and for 4 it is 100.
  5. Now, perform logical AND operations on the bits, going from left to right, and store the outcome in the corresponding location.
  6. The logical AND will treat 0 as False and 1 as True. Therefore the result will be false, and 0 will be the first bit of the result. The first bit of 3 is 0, and the first bit of 4 is 1.
  7. Over the course of the binary values, the same procedure is repeated. Since the second bits of 3 and 4 are 0 and 0, the second bit of the result will be saved as 0.
  8. Again, 0 will be the third and last bit of our result because the third and last bits of both 3 and 4 are 0 and 0.
  9. Therefore, 000 will be the final binary value of our result, which equals 0 when translated to integer decimal.

OR Operator:

The only difference between the bitwise AND and bitwise OR operators is that the bitwise OR operator performs logical OR rather than logical AND on the bit level, i.e., if at least one of the operands has 1, the result will also have 1 in the corresponding position, and 0 if they both have 0 in the corresponding position. The vertical bar or pipe sign, i.e. |, is used to indicate this.

Truth table for Bitwise OR operator in C++:

Num1 Num2 Result=Num1 | Num2
0 0 0
1 0 1
0 1 1
1 1 1

Example:

Output:

3 | 4 = 7

Working Mechanism:

The bitwise OR operation on 3 and 4 is carried out by the piece of code above. Let's take a closer look at how they operate.

  1. In binary terms, 4 is equal to 100 and 3 is equal to 11, respectively.
  2. By adding zeros to the left side of the most significant bit, the shortest binary value must first be converted to the length of the longest binary value.
  3. Now, we have 011 for 3 and 100 for 4.
  4. Now, perform logical OR operations on the bits, going from left to right, and store the outcome in the corresponding location.
  5. Since the initial bits of 3 and 4 are 0 and 1, respectively, the result's first bit is 1.
  6. Since the second bits of 3 and 4 are both 1 and 0, the second bit of the outcome is also 1.
  7. The third bit of the result is 1 because the third bits of 3 and 4 are both 1 and 0, respectively.
  8. Thus, the result's binary value is 111, which gets 7, when converted to decimal form.

XOR Operator:

The main distinction between this and the other two is that they conduct logical XOR on the bit level, i.e., the result will have 1 in the appropriate location if exactly one of the operands has 1 and the other has 0. If they both have the same bits, such as both 0s and both 1s, it will get 0 as a result.

Truth table for Bitwise XOR operator in C++:

Num1 Num2 Result=Num1^Num2
0 0 0
1 0 1
0 1 1
1 1 0

Example:

Output:

3 ^ 4 = 7

Working Mechanism:

The bitwise XOR operation on 3 and 4 is carried out by the snippet of code above. Let's take a closer look at how they operate.

  1. In binary terms, 4 is equal to 100 and 3 is equal to 11, respectively.
  2. We must first extend the shortest binary value to its full length by adding zeros to the most important bit's left side.
  3. The binary representation for 3 is now 011, and for 4 it is 100.
  4. Now, working from left to right, logically XOR the bits and then store the outcome in the corresponding location.
  5. Since the initial bits of 3 and 4 are 0 and 1, respectively, the result's first bit is 1.
  6. Since the second bits of 3 and 4 are both 1 and 0, the second bit of the outcome is also 1.
  7. The third bit of the result is 1 because the third bits of 3 and 4 are both 1 and 0, respectively.
  8. The result's binary value is 111, which gives us the number 7 when converted to decimal form.

Before moving on to other operators, let's look at the truth tables for the three bitwise operators in C++ that we have examined so far.

X Y x& y X | y X ^ y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Tab :- A common truth table in C++ for the bitwise AND, OR, and XOR operations.

Complement Operator:

If you've observed, the three bitwise operations we've seen so far were all binary operators, meaning that they each needed two operands to function. However, this one stands out since it is the only bitwise operator that just needs one operand. Other bitwise operations call for two operators.

The bitwise complement operator returns the one's complement of a single value. A number's one's complement is created by turning all of the binary value's 0s to 1s and all of the 1s to 0s. It is represented by the tilde sign, i.e. '~'.

Truth table for Bitwise Complement operator in C++:

Num1 Result = ~Num1
0 1
1 0

Example:

Output:

~5 = -6

Working Mechanism:

The code snippet above carries out the bitwise Complement operation on 5. Flipping all the bits results in 101, which is 010, or 2, in decimal notation.

Note:

  1. In this case, logic dictates that 5 = 2, but the compiler instead outputs the input value's complement of 2.
  2. Use 2's complement to store negative numbers.
  3. The binary code for 5 is 0000 0101.
  4. 1111 1010 is one's complement of 5.
  5. The binary code for 6 is 0000 0110.
  6. The number 1 to 6 is 1111 1001.
  7. To get the 2's complement of 1, add 1 to its one's complement (6 = 1111 1010). It is equal to 5.
  8. Hence, 6 is a 5's bitwise complement.

We've covered 4 bitwise operators in C++ so far. They all carried out the identical action on the bit level as the logical operators did on boolean variables, making them quite similar to the logical operators. However, the next two operators are significantly dissimilar from one another.

Left Shift (<<) Operator:

The Left Shift operator moves a given number of bits to the left in the bit pattern of an integer value. The Left Shift operator requires two operands: a value to be shifted, such as "x", and a second value, such as "n", indicating the number of bits that need to be shifted on the first value.

A compiler error stating "negative shift count" will be thrown if the value of "n" is negative. The value of "x" can be negative, but not that of "n". The Left Shift operation is applied on the number 2's complement when the value of 'x' is negative. Therefore, the number's sign may or won't match that of the left shift operation.

Two successive larger-than operators, or <<, are used to represent the Left Shift operator. If n is the operand to the operator's right, it is identical to multiplying the integer by 2 power n.

Why is it the case?

Many people ask that it is a very clear issue because the base value is multiplied by the new number when a number is moved to the left. When you shift the value of 3 to the left and add 0s to its rightmost end in decimal form, you are actually multiplying it by 10, which is its base value.

For instance, if you move 3 to the left by 1 place, you obtain 30, which is 3*10. Any basic value has the same properties. The left shift operator only operates on binary data. Therefore, the outcome is the same as multiplying by the powers of 2.

Syntax:

It has the following syntax:

Example:

Output:

5 << 2 = 20

Working Mechanism:

  1. The left shift operation is applied to the decimal value 5 in the aforementioned code snippet.
  2. It modifies the 5 by 2-bit patterns.
  3. 5 has a binary value of 101.
  4. The result is 10100 when you move it left by two locations and add two 0s to the least significant bit, which is at the right end of the binary sequence.
  5. The outcome of the binary-to-decimal conversion is 20.

Right Shift (>>) Operator

The main difference between the right shift and left shift operators is that the right shift operator shifts the bit to the right rather than the left. The remaining bits are changed from binary to an integer by popping the final n bits from the provided value.

The Right Shift operator follows the same guidelines as the Left Shift operator. A compiler error stating "negative shift count" will be thrown if the value of "n" is negative. The value of "x" can be negative, but not that of "n".

When using the right shift operator on a negative number, the 2's complement of the number is used, much like with the left shift operator.

As a result, when you conduct a right shift operation on a negative integer, the outcome will be a positive number since you replace the sign bit with 0 after moving the bit before it, which was 1, to the next location. Two successive less-than symbols, or >>, are used to indicate it.

Syntax:

It has the following syntax:

It is similar to dividing the given integer by its floor using the second power of n.

When you shift a number to the right, just as when you shift it to the left, you are actually dividing it by the base value. When you shift to the right and pop the last character from the number 345 in decimal form, you are actually dividing it by 10 at the rightmost end, which is its base value.

For instance, if you move 345 to the left by one position, you obtain 34, which is 345/10. Any basic value has the same properties. The outcome is identical to dividing by two powers of 2 because the Right Shift operator uses binary numbers.

Example:

Output:

20 >> 2 = 5

Working Mechanism:

  1. The Right Shift operation is applied to the decimal value 20 in the code snippet above.
  2. The bit patterns of 20 are moved by 2.
  3. 20 has a binary value of 10100.
  4. The result is 101 when you pop the final two bits and shift it to the right by two positions.
  5. When the outcome is transformed from binary to integer, it gets 5.

Conclusion:

In this article, you learned what bitwise operators are, how they differ from logical operators, and what they do in the C++. When utilized correctly, bitwise operations can help you save a tonne of time. Some of the most popular applications for bit manipulation techniques include:

  • Lowercase English characters are created by using the OR symbol (|) and the space bar.
  • Underlining converts English to uppercase when used with AND ('&').
  • For the case-swapping of English characters, use XOR "" and the space bar.
  • Switching two digits.
  • Verify if the supplied number has a 2 exponent.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA