The computer lacks the ability to comprehend the human-readable high-level language we use. Due to this limitation, a standardized approach was established to ensure that the computer could interpret any commands provided to it. Essentially, each command is translated into binary data, referred to as bits, at a fundamental level. The sequence of bits serves as a representation of a specific instruction.
A bit is characterized as the fundamental unit responsible for storing information in digital form.
Two values represent it as follows -
1 - It indicates the signal is present or True
0 - It indicates the signal is absent or False
Bits represent the digital state of each instruction. The sequence of bits follows a base of 2. Therefore, when interpreting a string of binary digits, it is essential to read from left to right, with the power of 2 incrementing accordingly.
Following a grasp of the fundamental concepts of bits, we will now delve into their manipulation within the C++ programming language.
Bit manipulation
Bit manipulation involves executing fundamental operations at the bit level of a numerical value comprising n digits. This technique is known for its efficiency and rudimentary nature, operating directly at the hardware level.
With that said, let's now delve into the fundamentals of bit manipulation in C++.
Logical AND
Logical AND functions by taking two inputs and yielding a true outcome only if both inputs are true. It is denoted by the symbol &&.
Examine the truth table of the AND operator.
In the final row, both A and B have elevated values, leading to a high overall output level.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 9;
// false && false = false
cout << ((a == 0) && (a > b)) << endl;
// false && true = false
cout << ((a == 0) && (a < b)) << endl;
// true && false = false
cout << ((a == 5) && (a > b)) << endl;
// true && true = true
cout << ((a == 5) && (a < b)) << endl;
return 0;
}
Output:
Logical OR
Logical OR provides a high output when at least one of the two operands is high. It is represented by the symbol ||.
Let's examine the truth table of the OR operator.
Here we observe the initial row where both input signals A and B are at a low level, leading to a low output of 0.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 9;
// false && false = false
cout << ((a == 0) || (a > b)) << endl;
// false && true = true
cout << ((a == 0) || (a < b)) << endl;
// true && false = true
cout << ((a == 5) || (a > b)) << endl;
// true && true = true
cout << ((a == 5) || (a < b)) << endl;
return 0;
}
Output:
Logical NOT
Logical NOT operates by taking a single operand and inverting its value. When the operand is false, it changes it to true, and conversely, when the operand is true, it changes it to false. This operation is represented by the symbol !.
Let's examine the truth table for the NOT operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 5;
// !false = true
cout << !(a == 0) << endl;
// !true = false
cout << !(a == 5) << endl;
return 0;
}
Output:
Left shift operator
The left shift operator shifts the value of the left operand to the left by the number of bits specified by the right operand.
It is denoted by <<.
C++ Program
_PRESERVE3__
Output:
Right shift operator
The right shift operator takes an operand and the value of the right operand is moved right by the number of bits specified by the right operand.
It is denoted by >>.
C++ Program
#include <bits/stdc++.h>
using namespace std;
int main()
{
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
// The result is 00000010
cout<< "a>>1: " << (a >> 1) << "\n";
// The result is 00000100
cout<< "b>>1: " << (b >> 1);
return 0;
}
Output: