C++ Program to Perform Bitwise Calculation using a Menu

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

Problem Description

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
advertisement
advertisement

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:
Syntax              Binary equivalent    Result
x = 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:
Syntax              Binary equivalent    Result
x = 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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.

Problem Solution

1. Take input from the user.
2. Now, use all six bitwise operators and perform the operations individually.

Program/Source Code
advertisement

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.

  1. /*
  2.  * C++ program to perform bitwise calculation using a menu
  3.  */
  4. #include <iostream>
  5. using namespace std;
  6. /*
  7.  * class declaration
  8.  */
  9. class bitwise_operator {
  10. public:
  11.     // function for bitwise AND
  12.     void bitwise_and(int a, int b)
  13.     {
  14.         cout << "a & b = " << (a & b) << "\n";
  15.     }
  16.     // function for bitwise OR
  17.     void bitwise_or(int a, int b)
  18.     {
  19.         cout << "a | b = " << (a | b) << "\n";
  20.     }
  21.     // function for bitwise XOR
  22.     void bitwise_xor(int a, int b)
  23.     {
  24.         cout << "a ^ b = " << (a ^ b) << "\n";
  25.     }
  26.     // function for bitwise NOT
  27.     void bitwise_not(int a, int b)
  28.     {
  29.         cout << "~a = " << (~a) << "\n";
  30.         cout << "~b = " << (~b) << "\n";
  31.     }
  32.     // function for bitwise Left Shift
  33.     void bitwise_leftshift(int a, int b)
  34.     {
  35.         cout << "a << b = " << (a << b) << "\n";
  36.     }
  37.     // function for bitwise Right Shift
  38.     void bitwise_rightshift(int a, int b)
  39.     {
  40.         cout << "a >> b = " << (a >> b) << "\n";
  41.     }
  42. };
  43. /*
  44.  * Main Function
  45.  */
  46. int main()
  47. {
  48.     int a, b, option;
  49.     char choice;
  50.     bitwise_operator o;
  51.     do {
  52.         cout << "Enter the value of a and b:\n";
  53.         cin >> a >> b;
  54.         cout << "\nBITWISE OPERATION:\n";
  55.         cout << "1.AND\n";
  56.         cout << "2.OR\n";
  57.         cout << "3.XOR\n";
  58.         cout << "4.NOT\n";
  59.         cout << "5.Left Shift\n";
  60.         cout << "6.Right Shift\n";
  61.         cout << "Select your option:\n";
  62.         cin >> option;
  63.         switch (option) {
  64.         case 1:
  65.             o.bitwise_and(a, b);
  66.             break;
  67.         case 2:
  68.             o.bitwise_or(a, b);
  69.             break;
  70.         case 3:
  71.             o.bitwise_xor(a, b);
  72.             break;
  73.         case 4:
  74.             o.bitwise_not(a, b);
  75.             break;
  76.         case 5:
  77.             o.bitwise_leftshift(a, b);
  78.             break;
  79.         case 6:
  80.             o.bitwise_rightshift(a, b);
  81.             break;
  82.         defualt:
  83.             cout << "Invalid option:\n";
  84.         }
  85.         cout << "\nDo you want to continue?(y/n)";
  86.         cin >> choice;
  87.     } while (choice == 'Y' || choice == 'y');
  88.     return 0;
  89. }
Program Explanation

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.

Runtime Test Cases
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.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.