Javatpoint Logo
Javatpoint Logo

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.

Bit manipulation C++

Bit

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.

Bit manipulation C++

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.

Bit manipulation C++

In the last row, A and B are high, resulting in a high output.

C++ Program

Output:

Bit manipulation C++
  • 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.

Bit manipulation C++

Here we can see the first row. Both inputs A and B are low, which results in 0(a low output).

C++ Program

Output:

Bit manipulation C++
  • 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.

Bit manipulation C++

C++ Program

Output:

Bit manipulation C++
  • 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

Output:

Bit manipulation C++
  • 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

Output:

Bit manipulation C++




Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA