Bit Commands and Operations in java

The Bit Process is a Process that is performed directly on the bits. Bit operations are generally used in data transfer operations.

 
Bit Processing Explanation
& AND
| OR
^ XOR
~ Reverse Bit (NOT)
>> Move A Bit Right
<< Move A Bit Left

 

int x=7;

x=x<<1;

In the bit operation, the bit structure of the variable x is shifted one bit to the left. Bit equivalent of 7 is 0000000000000000000000000000111. when we shift one bit to the left, the new value will be 000000000000000000000000000001110. the int equivalent of this value is 14.
 

public class Main
{
	public static void main(String[] args) {
	    int x=5;
		System.out.println("x="+x);
		x=x<<1;
		System.out.println("x="+x);
		x=x<<1;
		System.out.println("x="+x);
		x=x>>2;
		System.out.println("x="+x);
		x=~x;
		System.out.println("x="+x);
	}
}

 

output:

x=5
x=10
x=20
x=5
x=-6

 

Tagged In:

Software developer

26 Total Posts