English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
bitwise operators ++In this tutorial, we will learn about C
In C, bitwise operators. ++In C, bitwise operators perform operations on individual bit-level integer data. These operations include testing, setting, or shifting actual bits. For example,
a & b; a | b;
This is C ++Contains6A list of bitwise operators.
Operator | Name | Description | Example |
---|---|---|---|
& | Bitwise AND | If a bit exists in both operands, the binary AND operator copies a bit to the result. | (A & B) will be 12which is 0000 1100 |
| | bitwise OR | If a bit exists in either operand, the binary OR operator copies a bit to the result. | (A | B) will be 61which is 0011 1101 |
^ | Bitwise XOR | If a bit exists in one operand but not in both, the binary XOR operator copies a bit to the result. | (A ^ B) will be 49which is 0011 0001 |
~ | change 0 to | The binary complement operator is a unary operator with a 'flip' bit effect, that is, 0 becomes1,1and becomes 0. | (~A) will be -61which is 1100 0011which is the two's complement form of a signed binary number. |
<< | Left shift | Binary left shift operator. The value of the left operand is shifted to the left by the number of bits specified by the right operand. | A << 2 will be obtained 240, which is 1111 0000 |
>> | Right shift | Binary right shift operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand. | A >> 2 will be obtained 15which is 0000 1111 |
These operators are necessary because the arithmetic logic unit (ALU) in the computer CPU performs arithmetic operations at the bit level.
Note:Bitwise operators can only be used with char and int data types.
only when both operands are1The bitwise AND & operator returns1. Otherwise, it will return 0.
The following table demonstrates how the bitwise AND operator works. Assuming a and b are two numbers that can only take binary values,1and the operands of 0.
a | b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Note:The table is called the 'truth table' when referred to by the bitwise AND operator.
Let's look at two integers12and25Bitwise AND operation:
12 = 00001100 (binary) 25 = 00011001 binary) //12and25Bitwise AND operation 00001100 & 00011001 _________ 00001000 = 8 (decimal)
#include <iostream> using namespace std; int main() { //Declare variables int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a & b = " << (a & b) << endl; return 0; }
Output result
a = 12 b = 25 a & b = 8
In the above example, we declared two variables a and b. Please note this line here,
cout << "a & b = " << (a & b) << endl;
Here, we perform a bitwise AND operation (&) between variables a and b.
if at least one operand is1then the bitwise OR (|) operator returns1. Otherwise, return 0.
The truth table below demonstrates how the bitwise OR operator works. Assumeaandbcan only take binary values (i.e.1or 0) ofoperands.
a | b | a | b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Let's look at two integers12and25bitwise OR operation:
12 = 00001100 (binary) 25 = 00011001 binary) //12and25bitwise OR operation 00001100 | 00011001 _________ 00011101 = 29 (decimal)
#include <iostream> int main() { int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a | b = " << (a | b) << endl; return 0; }
Output result
a = 12 b = 25 a | b = 29
inbitwise ORwhere a = 12and b = 25results in29.
when and only when one of the operands is1then the bitwise XOR ^ operator returns1. However, if both operands are 0, or both are1, then the result is 0.
The truth table below demonstrates how the bitwise OR operator works. Assumeaandbcan only take binary values (i.e.1or 0) ofoperands.
a | b | a ^ b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Let's look at two integers12and25bitwise XOR operation:
12 = 00001100 (binary) 25 = 00011001 binary) //12and25bitwise XOR operation 00001100 ^ 00011001 _________ 00010101 = 21 (decimal)
#include <iostream> int main() { int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a ^ b = " << (a ^ b) << endl; return 0; }
Output result
a = 12 b = 25 a ^ b = 21
a = 12and b = 25bitwise XOR operation results in21.
bitwise complement operator1The bitwise complement operator is a unary operator (acts on a single operand). It is represented by ~ and changes a binary number1.
bitwise complement -It should be noted that the bitwise complement of any integer N is+1For example, (N
For example, an integer35. According to the rules,35bitwise complement should be-(35 +1) = -36. Now, let's see if we get the correct answer.
35 = 00100011 binary) // using the bitwise complement operator ~ 00100011 __________ 11011100
In the above example, we conclude that00100011(35)two's complement is11011100. If the result is converted to decimal, then we get220.
However, it should be noted that we cannot directly convert the result to decimal to obtain the desired output. This is because the binary result11011100which is also equivalent to-36.
To understand this, we first need to calculate-36binary output. We use2to calculate the binary representation of negative integers.
In binary arithmetic,1two's complement of1,1If we change 0 to1and change 0 to1If we add 1 to the two's complement result of2two's complement.
For example,
36 = 00100100 (binary) 1two's complement = 11011011 2The two's complement : 11011011 + 1 _________ 11011100
Here, we can see36The2The complement (i.e.-36) is1101110The two's complement of35Therefore, we can say
is35The bit complement of-36.
#include <iostream> int main() { int num1 = 35; int num2 = -150; cout << "~(" << num1 << ") = " << (~num1) << endl; cout << "~(" << num2 << ") = " << (~num2) << endl; return 0; }
Output result
~,35) = -36 ~,-150) = 149
In the above example, we declared two integer variables num1and num2, and initialize them separately with values35and-150
Then we initialize them separately with code (~num1) and (~num2) calculate their two's complement and display them on the screen.
35The two's complement of ~ - (35 + 1) = -36 i.e. ~35 = -36 -15The two's complement of 0 = - (-150 + 1) = - (-149) = 149 i.e. ~(-150) = 149
This is exactly what we get in the output.
C ++There are two shift operators in programming:
Right shift operator >>
Left shift operator <<
The right shift operator shifts all bits to the right by a certain number of specified bits. It is represented by >>.
When we shift any number to the rightLeast significant bitwill be discarded, andMost significant bitwill be replaced by zero.
From the above figure, we can see that we have4bit number. When we perform1bit.
The result, the rightmost bit is discarded (Discarded), and the leftmost bit remains empty. This empty bit is replaced by0replacing (Replacement Bit).
The left shift operator shifts all bits to the left by a certain number of specified bits. It is represented by <<.
From the above figure, we can see that we have4bit number. When we perform1When performing a left shift operation on a bit, each individual bit shifts to the left1bit.
The result, the leftmost bit is discarded (Discarded), and the rightmost bit remains empty. This empty bit is replaced by0replacing (Replacement Bit).
#include <iostream> int main() { //Declare two integer variables int num = 212, i; //Right shift operation cout << "Right shift:\n" << endl; //Use a for loop to shift the num from the 0th bit to the right3bit for (i = 0; i < 4; i++) { cout << "212 >> " << i << " = " << (212 >> i) << endl; } //Left shift operation cout << "\nLeft shift:\n" << endl; //Use a for loop to shift the num from the 0th bit to the left3bit for (i = 0; i < 4; i++) { cout << "212 << " << i << " = " << (212 << i) << endl; } return 0; }
Output result
Right shift: 212 >> 0 = 212 212 >> 1 = 106 212 >> 2 = 53 212 >> 3 = 26 Left shift: 212 << 0 = 212 212 << 1 = 424 212 << 2 = 848 212 << 3 = 1696
From the output of the above program, we can infer that for any numberNThe results of the right shift operators are all:
N >> 0 = N N >> 1 = (N >> 0) / 2 N >> 2 = (N >> 1) / 2 N >> 3 = (N >> 2) / 2
etc.
Similarly, the result of the left shift operator is:
N << 0 = N N << 1 = (N << 0) * 2 N << 2 = (N << 1) * 2 N << 3 = (N << 2) * 2
etc.
Therefore, we can conclude that,
N >> m = [ N >> (m-1) ] / 2 N << m = [ N << (m-1) ] * 2