Bit manipulation C++
The computer does not understand the high-level language in which we communicate. For these reasons, there was a standard method by which any instruction given to the computer was understood. At an elementary level, each instruction was sent into some digital information known as bits. The series of bits indicates that it is a particular instruction.
A bit is defined as the basic unit which stores the data in digital notation.
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 logical state of any instruction. The series of bits have a base which is 2. Thus if we say if we have a series of binary digits, it is read from left to right, and the power of 2 increases.
So after understanding the basics of bit, let us understand its manipulation in C++.
Bit manipulation is defined as performing some basic operations on bit level of n number of digits. It is a speedy and primitive method as it directly works at the machine end.
With that, let us get into the basics of bit manipulation in C++.
Logical AND takes two operands and returns true if both of them are true. The sign is &&.
Let us look at the truth table of the AND operator.
In the last row, A and B are high, resulting in a high output.
Logical OR gives us high output if either of the input of the two operands is high. The symbol is ||
Let us look at the truth table of the OR operator.
Here we can see the first row. Both inputs A and B are low, which results in 0(a low output).
Logical NOT is taking only one operand and reverts it. If the operand is low, it makes it high and vice versa. The symbol is !.
Let us look at the truth table of the NOT operator.
The left shift operator takes an operand and the value of the left operand is moved left by the number of bits specified by the right operand.
It is denoted by <<.
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 >>.