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

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

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.

C++ Program

Example

#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 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).

C++ Program

Example

#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 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.

C++ Program

Example

#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 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 <<.

C++ Program

Example

#include<iostream>

using namespace std;

int main()

{

	// a = 5(00000101), b = 9(00001001)

	unsigned char a = 5, b = 9;

	// The result is 00001010

	cout << "a<<1: "<<  (a<<1) << "\n";

	// The result is 00010010

	cout << "b<<1: " <<  (b<<1);

	return 0;

}

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

Example

#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:

Input Required

This code uses input(). Please provide values below: