Bitwise operators in Java

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

Bitwise operators in Java

Post by swathi » Wed Dec 05, 2018 9:51 am

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

Post by charan » Wed Dec 05, 2018 9:54 am

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)

Post Reply

Return to “Java Operators”