This is a C++ Program to Perform Bitwise Calculation Using a Menu (4-bit numbers).

As we know our computer stores everything in the form of bits and it will be very easy for the computer if we work on bits. Bitwise operations are faster and closer to the system and also optimize the program to a good level. Through bitwise operators, we can perform bitwise operations.

**Bitwise Operators in C++:**

**1. & (bitwise AND)**

Takes two numbers as operands and the result is true only when both the operands are true.

For example: X = (5)_{10}= (101)_{2}Y = (3)_{10}= (011)_{2}X & Y = (101)_{2}& (011)_{2}X & Y = (001)_{2}= (1)_{10}

**2. | (bitwise OR)**

Takes two numbers as operands and the result is true if any of the operands is true.

For example: X = (5)_{10}= (101)_{2}Y = (3)_{10}= (011)_{2}X | Y = (101)_{2}| (011)_{2}X | Y = (111)_{2}= (7)_{10}

**3. ^ (bitwise XOR)**

Takes two numbers as operands and the result is true only if one of its operands is true.

It is mainly used to toggle certain bits. It also helps in swapping two variables without taking the third variable.

For example: X = (5)_{10}= (101)_{2}Y = (3)_{10}= (011)_{2}X ^ Y = (101)_{2}^ (011)_{2}X ^ Y = (110)_{2}= (6)_{10}

**4. ~ (bitwise NOT)**

Takes one number and inverts all its bits.

For example: N = (5)_{10}= (101)_{2}~N = ~ (5)_{10}= ~ (101)_{2}= (010)_{2}= (2)_{10}

**5. << (Left Shift)**

Takes two number as operands and left shift some number of bits of operand one, the second operand decides the number of places to shift and append 0 at the end. Left shift is equivalent to multiplying the first operand with (2-second operand).

For example:SyntaxBinary equivalentResultx = 3; 00000011 3 x = x<<1; 00000110 6 x = x<<3; 00110000 48 x = x<<2; 11000000 192

First x was 3 and in next step the bit was shifted by 1. The value of x gets multiplied by 2^1 and we append 0 at the right most end thereby shifting the bits by 1. So the new value of x becomes 6. Next time x is 6 and we are shifting 3 bits now. So we'll multiply 6 by 2^3 and append 3 zeros at the right most end. So the resultant value of x becomes 48. This way it'll continue.

**6. >> (Right Shift)**

Takes two number as operands and right shift some number of bits of operand one, the second operand decides the number of places to shift. Right shift is equivalent to dividing the first operand with (2-second operand).

For example:SyntaxBinary equivalentResultx = 192; 11000000 192 x = x>>1; 01100000 96 x = x>>3; 00001100 12 x = x>>2; 00000011 3

First x was 192 and in next step the bit was shifted by 1. The value of x gets divided by 2^1 and we append 0 at the left most end thereby shifting the bits by 1. So the new value of x becomes 96. Next time x is 96 and we are shifting 3 bits now. So we'll divide 96 by 2^3 and append 3 zeros at the left most end. So the resultant value of x becomes 12. This way it'll continue.

**Truth Table for OR, AND, XOR and NOT:**

X | Y | X|Y | X&Y | X^Y | ~X |
---|---|---|---|---|---|

0 | 0 | 0 | 0 | 0 | 1 |

0 | 1 | 1 | 0 | 1 | 1 |

1 | 0 | 1 | 0 | 1 | 0 |

1 | 1 | 1 | 1 | 0 | 0 |

**Drawbacks:**

In Left Shift and Right Shift, the operands should not be negative. If any of the operands is a negative number, its result in undefined behaviour.

Also, if the number is shifted more than the size of integer, the behaviour is undefined.

For example:

5 << 35 is undefined if integers are stored using 32 bits.

1. Take input from the user.

2. Now, use all six bitwise operators and perform the operations individually.

Here is the source code of the C Program to Perform Bitwise Calculation Using a Menu (4-bit numbers). The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

`/*`

`* C++ program to perform bitwise calculation using a menu`

`*/`

`#include <iostream>`

using namespace std;

`/*`

`* class declaration`

`*/`

class bitwise_operator {

public:

`// function for bitwise AND`

void bitwise_and(int a, int b)

`{`

cout << "a & b = " << (a & b) << "\n";

`}`

`// function for bitwise OR`

void bitwise_or(int a, int b)

`{`

cout << "a | b = " << (a | b) << "\n";

`}`

`// function for bitwise XOR`

void bitwise_xor(int a, int b)

`{`

cout << "a ^ b = " << (a ^ b) << "\n";

`}`

`// function for bitwise NOT`

void bitwise_not(int a, int b)

`{`

cout << "~a = " << (~a) << "\n";

cout << "~b = " << (~b) << "\n";

`}`

`// function for bitwise Left Shift`

void bitwise_leftshift(int a, int b)

`{`

cout << "a << b = " << (a << b) << "\n";

`}`

`// function for bitwise Right Shift`

void bitwise_rightshift(int a, int b)

`{`

cout << "a >> b = " << (a >> b) << "\n";

`}`

};

`/*`

`* Main Function`

`*/`

int main()

`{`

int a, b, option;

char choice;

`bitwise_operator o;`

do {

cout << "Enter the value of a and b:\n";

cin >> a >> b;

cout << "\nBITWISE OPERATION:\n";

cout << "1.AND\n";

cout << "2.OR\n";

cout << "3.XOR\n";

cout << "4.NOT\n";

cout << "5.Left Shift\n";

cout << "6.Right Shift\n";

cout << "Select your option:\n";

cin >> option;

switch (option) {

case 1:

o.bitwise_and(a, b);

break;

case 2:

o.bitwise_or(a, b);

break;

case 3:

o.bitwise_xor(a, b);

break;

case 4:

o.bitwise_not(a, b);

break;

case 5:

o.bitwise_leftshift(a, b);

break;

case 6:

o.bitwise_rightshift(a, b);

break;

`defualt:`

cout << "Invalid option:\n";

`}`

cout << "\nDo you want to continue?(y/n)";

cin >> choice;

} while (choice == 'Y' || choice == 'y');

return 0;

`}`

1. Create class bitwise_operator and add all the functions (bitwise_and(), bitwise_or(), bitwise_not(), bitwise_xor(), bitwise_leftshift() and bitwise_rightshift() ) with its definition.

2. Create an object of a class inside the main function.

3. Get the input from the user for two operands.

4. Now, get the user option and match with the switch case. The matched case will call the bitwise_operator class’s function through the object created. Also, the variables will be the parameter of the function.

5. The called function will display its result and the operation performed.

6. default: in switch case will simply print the message.

7. Get the choice from the user.

8. If the choice match with the condition in do while loop repeats the steps from step number three, till the loops breaks.

Enter the value of a and b: 5 3 BITWISE OPERATION: 1.AND 2.OR 3.XOR 4.NOT 5.Left Shift 6.Right Shift Select your option: 1 a & b = 1 Do you want to continue?(y/n)y Enter the value of a and b: 5 3 BITWISE OPERATION: 1.AND 2.OR 3.XOR 4.NOT 5.Left Shift 6.Right Shift Select your option: 2 a | b = 7 Do you want to continue?(y/n)y Enter the value of a and b: 5 3 BITWISE OPERATION: 1.AND 2.OR 3.XOR 4.NOT 5.Left Shift 6.Right Shift Select your option: 3 a ^ b = 6 Do you want to continue?(y/n)y Enter the value of a and b: 6 3 BITWISE OPERATION: 1.AND 2.OR 3.XOR 4.NOT 5.Left Shift 6.Right Shift Select your option: 5 a << b = 48 Do you want to continue?(y/n)y Enter the value of a and b: 192 1 BITWISE OPERATION: 1.AND 2.OR 3.XOR 4.NOT 5.Left Shift 6.Right Shift Select your option: 6 a >> b = 96 Do you want to continue?(y/n)n

**Sanfoundry Global Education & Learning Series – 1000 C++ Programs.**

Here's the list of Best Books in C++ Programming, Data Structures and Algorithms.

**Next Steps:**

- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Apply for C++ Internship
- Apply for Computer Science Internship
- Buy Computer Science Books
- Buy C++ Books
- Buy Programming Books