## Bitwise operators in Java

Types of Java Operators, Operator Precedence, Arithmetic Operators, Comparison or Relational Operators, Logical Operator and Bitwise Operators etc.
swathi
Posts: 134
Joined: Fri Nov 23, 2018 2:52 am

### Bitwise operators in Java

What are the Bitwise operators in Java Explain In Detail ?

charan
Posts: 48
Joined: Fri Nov 23, 2018 5:31 am

### Re: Bitwise operators in Java

Bitwise operators in Java :

Bitwise operators are used to perform manipulation of individual bits of a number. They can be used with any of the integral types (char, short, int, etc). They are used when performing update and query operations of Binary indexed tree.

1. Bitwise OR (|) –
This operator is binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e, if either of the bits is 1, it gives 1, else it gives 0.
For example,

a = 5 = 0101 (In Binary)
b = 7 = 00011001 (In Binary)

Bitwise OR Operation of 5 and 7
0101
| 0111
________
0111 = 7 (In decimal)

2. Bitwise AND (&) –
This operator is binary operator, denoted by ‘&’. It returns bit by bit AND of input values, i.e, if both bits are 1, it gives 1, else it gives 0.
For example,

a = 5 = 0101 (In Binary)
b = 7 = 00011001 (In Binary)

Bitwise AND Operation of 5 and 7
0101
& 0111
________
0101 = 5 (In decimal)

3. Bitwise XOR (^) –
This operator is binary operator, denoted by ‘^’. It returns bit by bit XOR of input values, i.e, if corresponding bits are different, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 00011001 (In Binary)

Bitwise XOR Operation of 5 and 7
0101
^ 0111
________
0010 = 2 (In decimal)

4. Bitwise Complement (~) –
This operator is unary operator, denoted by ‘~’. It returns the one’s compliment representation of the input value, i.e, with all bits inversed, means it makes every 0 to 1, and every 1 to 0.
For example,

a = 5 = 0101 (In Binary)
b = 7 = 00011001 (In Binary)

Bitwise Compliment Operation of 5 and 7

~ 0101
________
1010 = 10 (In decimal)

satyanarayanareddy
Posts: 24
Joined: Thu Jan 03, 2019 3:54 am

### Re: Bitwise operators in Java

Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

Bitwise operator works on bits and performs the bit-by-bit operation.
Assume if a = 60 and b = 13; now in binary format they will be as follows −

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011