Bitwise operators in java with example

Bitwise used for manipulation of data in bit level.

Operator Function
` bitwise NOT
<< left shift
>> right shift
>>> shift right zero fill
& bitwise AND
^ bitwise XOR
| bitwise OR
&= bitwise AND assign
^= bitwise XOR assign
|= bitwise OR assign

Bitwise not applicable for float, double and char.

Syntax:-

syntax for & | and ^

value1 bitwise value2

Example:-
2 & 3
num1 & num2

Here value1 and value2 must be integer data type


syntax for >> and <<

value bitwise number_of_shift

Example:-
6 >> 2
num >> 3

Here value and number_of_shift  must be integer data type

Note:-
# Left shift by 2 is equal to multiply by 2
# Right shift by 2 is equal to division by 2
# <<< operator is not used

Example:-

Program for shift left

import java.util.Scanner;
class group{
public static void main(String arg[]){
int num;
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num = data.nextInt();
num=num<<2;
System.out.println("Answer:"+num);
}
}

Output:-
Enter a number
5
answer: 20

 5 is in binary 101 when we shift left we put zero at right side. Here we shifting by 2 so we put 2 zero at right side 10100 ie 20


Previous Code:-

List Code:-

Next Code:-

Java Programs:-

Leave reply

Add your comments here

Back to Top