Computers cannot understand the high-level languages we use. Therefore, to make computers understand, there is a standard method of converting given instructions into some numerical information called bits. The sequence of bits represents specific instructions.
Bits
A bit is defined as the basic unit of data storage in numeric form.
It has two values, represented as follows:
1 – Indicates that the signal is present or true
0 – Indicates that the signal is absent or false
A bit represents the logical state of any instruction. The sequence of bits has a base of 2. Therefore, if we have a series of binary digits, it is read from left to right, and the power of 2 increments.
Now that we understand the basics of bits, let’s learn about bit manipulation in C++.
Bit Manipulation
Bit manipulation is defined as performing some basic operations at the bit level on n numbers. It is a fast and fundamental method because it works directly at the machine level.
Let’s dive into the basics of bit manipulation in C++.
👇 Click to Claim 👇
👉 C Language Knowledge Resource Collection
Logical AND
Logical AND takes two operands and returns true when both are true. The symbol is &&.
Let’s look at the truth table for the AND operator.
In the last row, both A and B are high, resulting in a high output.
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 gives a high output when either of the two operands is high. The symbol is ||.
Let’s look at the truth table for the OR operator.
Here we can see in the first row. Inputs A and B are both low, resulting in 0 (low output).
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 takes only one operand and inverts it. If the operand is low, it makes it high, and vice versa. The symbol is !.
Let’s look at 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 takes one operand and shifts the value of the left operand to the left by a specified number of bits.
It is represented by <<.
C++ Program
#include<iostream>
using namespace std;
int main() {
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
// Result is 00001010
cout << "a<<1: " << (a<<1) << "\n";
// Result is 00010010
cout << "b<<1: " << (b<<1);
return 0;
}
Output:
Right Shift Operator
The right shift operator takes one operand and shifts the value of the right operand to the right by a specified number of bits.
It is represented by >>.
C++ Program
#include <bits/stdc++.h>
using namespace std;
int main() {
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
// Result is 00000010
cout<< "a>>1: " << (a >> 1) << "\n";
// Result is 00000100
cout<< "b>>1: " << (b >> 1);
return 0;
}
Output:
Popular Recommendations
-
CLion Tutorial – Path Variables in CLion
-
C Language Algorithm – “Maximum Depth of Binary Tree” Algorithm Problem
-
C++ Tutorial – Detailed Explanation of the Difference Between Constructor and Destructor in C++