Bitwise Operator In C++ with example

Bitwise Operators ?

A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.

Bitwise Operators are used in:

Communication stacks where the individual bits in the header attached to the data signify important information.

Embedded software for controlling different functions in the chip and indicating the status of hardware by manipulating the individual bits of hardware registers of embedded microcontrollers.

Low-level programming for applications such as device drivers, cryptographic software, video decoding software, memory allocators, compression software and graphics etc.

Maintaining large sets of integers efficiently in search and optimization problems.

Bitwise operations performed on bit flags, which can enable an instance of enumeration type to store any combination of values defined in an enumerator list.

Bitwise Operators

Operator Description
<< Binary left Shift Operator
>> Binary Right Shift Operator
~ Binary One's Complement Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator

Example 1:

Output :

$ gcc test.cpp
$ a.out
Bitwise Operators
a & b = 5
a | b = 7
a ^ b = 2
~a = -8
~b = -6
a >> b = 0
a << b = 224

Example 2:

Output :

result 1 - Value of c is : 12
result 2 - Value of c is:  61
result 3 - Value of c is:  49
result 4 - Value of c is: -61
result 5 - Value of c is:  240
result 6 - Value of c is:  15