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.
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++:
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.
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++:
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.
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++:
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.
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.
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++:
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:
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:
Right Shift (>>) OperatorThe 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:
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:
Next TopicHow Many Indicators are Available in C++
|