Bitwise Operator In Java

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 :

num1 & num2: 2
num1 | num2: 31
num1 ^ num2: 29
~num1: -12
num1 << 2: 44 num1 >> 2: 2
a >> b = 0
a << b = 164

Example 2:



Output :

a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5