# C++ Bitwise Operators

Most high-level language does not allow a bitwise operation because it is a machine level task. The bit is manipulated in memory to get the desired results using bitwise operators.

The list of bitwise operators are as follows

## Bitwise AND

A bitwise operation happens between two bits. A number is converted into a bit string and then bitwise operations performed on them.

The bitwise AND works similarly as logical AND. All output combination is show for bitwise AND below.

Let A and B be two variables with some one bit value, then:

The only time output is 1 when both A and B are 1.

For example, bitwise AND with 12 and 7 will result in following:

``````1 1 0 0  // Binary value of 12
0 1 1 1 //  Binary value of 7
----------
0 1 0 0
----------
``````

The result is number 4.

### Example Program: Bitwise AND

We now illustrate the correctness of the bitwise AND through a program.

``````#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

//Variable Declaration

unsigned int a, b;
unsigned int result;
//Variable initialization

a = 12;
b = 7;

result = a & b;
//printing the results

cout << " Output = " << " " << result << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
``````

### Output:

``````Output = 4
``````

## Bitwise OR

The bitwise OR is similar to logical OR you learned earlier, except that it works at bit level. When a bitwise OR is performed on two bits – A and B, then the result is 1 if the bit value of A or B is 1. The bitwise operations are possible for more than two bits.

All combination of bit values is given below for bitwise OR.

For example,

If you want to perform bitwise OR operation on A = 14 and B = 3 then

``````1 1 1 0 // Binary value of 14
0 0 1 1 // Binary value of 3
----------
1 1 1 1
-----------``````

Therefore, the output is 15.

### Example Program: Bitwise OR

To verify the output from above example, we will write a program that computes using bitwise OR.

``````#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
//Variable declarations

int a,b,result;

//Variable initialization

a = 14;
b = 3;
result = 0;

// Bitwise OR operation

result = a | b;

//printing the results

cout << "Output =" << " " << result << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
``````

### Output:

``Output = 15``

The output is what we expected.

## Bitwise XOR (Exclusive-OR)

The bitwise XOR is a special kind of logical operation where two single bit inputs – A and B gives an output of 1 if either of them if is 1. Otherwise, the result is 0.

All combination of two inputs and a single output is given for bitwise XOR below.

Note how the output is 1 when either input is 1.

Let us see a manual example for bitwise XOR and later create a program that performs bitwise XOR to verify our output.

If A = 24 and B = 45 then bitwise XOR will result in following result.

``````0 0 0 1 1 0 0 0 // Binary value of 24
0 0 1 0 1 1 0 1 // Binary value of 45
-----------------
0 0 1 1 0 1 0 1
------------------
``````

Therefore, the ouput 0011 0101 is binary equivalent of 53.

### Example Program: Bitwise XOR

``````#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
//Variable declarations

int a,b,result;

//Variable initialization

a = 24;
b = 45;
result = 0;

// Bitwise OR operation

result = a ^ b;

//printing the results

cout << "Output =" << " " << result << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
``````

### Output:

``Output = 53``

## Left Shift Operator (<<)

The value of the variable can be modified by changing the bits. This is achived using bitwise shift operators.

The left shift operator modifies the value of variable by shifting the leftmost bits.

For example,

If A = 23 and you want to change the value of A using left shift operator,

0001 0111 // Binary value of 23

0010 1110 // Binary value of 46 after 1 bit left shift

Therefore, after shifting a single bit to the left, we get 46.

### Example Program: Left Shift

``````#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
//Variable Declaration
int A;

// Variable initialization

A = 23;

//Bitwise Left Shift

A = A << 1;

// Printing the results

cout << "Output = " << " " << A << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
``````

### Output:

``Output = 46``

It is not necessary that you have to shift only one bit, you can shift more than one to achive the desired results.

## Right Shift Operator(>>)

The right shift operator is similar to left shift operator and it modify the value of a variable by shifting the rightmost bits.

For example,

If A = 23 and you want to shift rightmost bit then,

0001 0111 //Binary value of 23

0000 1011 // Binary value of 11 after shift one bit to the right

After shift 1 bit to the right we get 11.

### Example Program: Right Shift Operator(>>)

``````#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
//Variable Declaration
int A;

// Variable initialization

A = 23;

//Bitwise Left Shift

A = A >> 1;

// Printing the results

cout << "Output = " << " " << A << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
``````

### Output:

``Output = 11``

## Complement(~)

The complement inverts the bits of a variable. If A = 44 with binary value 0010 1100 then the 1’s complement binary value is 1101 0011 which is 128 + 64 + 16 + 3 = 211.

The compiler gives the output in 2’s complement. To convert 1101 0011  into 2’s complement.

``````1101 0011

00000001

-------------------------------------------

1101 0100 // signed binary value of -45

---------------------------------------------``````

### Example Program: Complement

``````#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
//Variable Declaration
short int A;

// Variable initialization

A = 45;

//Bitwise Left Shift

A = ~A;

// Printing the results

cout << "Output = " << " " << A << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
``````

### Output:

``Output = -45``