Bitwise Operators in C++

Bitwise Operators in C++
Techiio-author
Written by Sagar RabidasFebruary 6, 2022
12 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Bitwise Operators in C++

Bitwise Operators in C++

Little by the little operation is performed, and the operator that works on bits is referred to as a bitwise operator. Using bitwise operators, there aren't any byte-stage operations in programming; most effective bit-stage calculations are carried out in programming. The bits may be manipulated by the usage of diverse bitwise operators. The operations of bitwise operators may be carried out on integer and person datatypes only. Bitwise operators can't be operated at the glide and double. In c++, there is a complete), bitwise xor (^), left shift (<<), right shift (>>), and bitwise no longer (~).

Operators of Bitwise in C++

In C++, there are a total of six bitwise operators. They are:

  • Bitwise AND (&):-

In bitwise and (&) operation, two numbers are taken as operands, an and operation is carried out on every bit of numbers. If both the bits are one, the result of and operation is one. If both the bits are 0, the result of and operation is zero. If all people of the bits are 0, the result of the operation is zero. Let’s take a simple instance for bitwise and operation.

Code:

#include <iostream>
using namespace std;
int main()
{
unsigned int num1 = 10;          // 10 = 0000 1010
unsigned int num3 = 12;          // 12 = 0000 1100
int num2 = 0;
num2 = num1 & num3;         // 8 = 0000 1000
cout << "Value of num2 is : " << num2 << endl ;
return 0;
}
  • Bitwise OR (|):-

The) operation, two numbers are taken as operands, and or operation is executed on each little bit of two numbers. If both the bits are one, the result of or operation is one. If each of the bits is zero, the result of the operation is 0. If everybody of the bits is one, the result of or operation is one. Allow’s take a simple example for bitwise or operation.

Code:

#include <iostream>
using namespace std;
int main()
{
unsigned int num1 = 10;          // 10 = 0000 1010
unsigned int num3 = 12;          // 12 = 0000 1100
int num2 = 0;
num2 = num1 | num3;         // 14 = 0000 1110
cout << "Value of num2 is : " << num2 << endl ;
return 0;
}
  • Bitwise XOR (^):-

In the bitwise xor (^) operation, two numbers are taken as operands, and the xor operation is performed on each bit of two numbers. If both the bits are zero, the result of the xor operation is 0. If everybody of the bits is one or each the bits are one, the result of the xor operation is one. Permit’s take a simple example for bitwise xor operation.

Code:

#include <iostream>
using namespace std;
int main()
{
unsigned int num1 = 10;  // 10 = 0000 1010
unsigned int num3 = 12;  // 12 = 0000 1100
int num2 = 0;
num2 = num1 ^ num3;      // 6 = 0000 0110
cout << "Value of num2 is : " << num2 << endl ;
return 0;
}
  • Left Shift (<<) operation:-

In the left shift (<<) operation, two numbers are taken as operands. The first operand left shifts the bits, and the second one operand decides on the range of places to be shifted. Let’s take an easy instance for the left shift (<<) operation.

Code:

#include <iostream>
using namespace std;
int main()
{
unsigned int num1 = 12; // 12 = 0000 1100
int num2 = 0;
num2 = num1 << 2; // 48 = 0011 0010
cout << "Value of num2 is: " << num2 << endl ;
return 0;
}
  • Right Shift (>>) operation:-

In the proper shift (>>) operation, numbers are taken as operands. The second operand makes a decision the first operand right shifts the bits, and the number of locations to be shifted. Let’s take an easy instance of the proper shift (>>) operation.

Code:

#include <iostream>
using namespace std;
int main()
{
unsigned int num1 = 12; // 12 = 1100
int num2 = 0;
num2 = num1 >> 2; // 3 = 0011
cout << "Value of num2 is: " << num2 << endl ;
return 0;
}
  • Bitwise NOT (~):-

In the Bitwise NOT (~) operation, one number is taken as an operand, and all the bits of the number are inverted. Let’s take a simple example for Bitwise NOT (~) operation.

Code:

#include <iostream>
using namespace std;
int main()
{
// 12 = 0000 1100
unsigned int num1 = 12;
int num2 = 0;
num2 = ~num1;
cout << "Value of num2 is: " << num2 << endl ;
return 0;
}
  • All the bitwise operations:-

Let’s take a simple program including all the bitwise operations,

Code:

#include <iostream>
using namespace std;
main() {
unsigned int a = 60;      // 60 = 0011 1100
unsigned int b = 13;      // 13 = 0000 1101
int c = 0;
c = a & b;             // 12 = 0000 1100
cout << "Line 1 - Value of c is : " << c << endl ;
c = a | b;             // 61 = 0011 1101
cout << "Line 2 - Value of c is: " << c << endl ;
c = a ^ b;             // 49 = 0011 0001
cout << "Line 3 - Value of c is: " << c << endl ;
c = ~a;                // -61 = 1100 0011
cout << "Line 4 - Value of c is: " << c << endl ;
c = a << 2;            // 240 = 1111 0000
cout << "Line 5 - Value of c is: " << c << endl ;
c = a >> 2;            // 15 = 0000 1111
cout << "Line 6 - Value of c is: " << c << endl ;
return 0;
}

C
C++
Bitwise Operators
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance