Doubts in operation

2

Could someone give me a brief explanation about the results of these operations?

System.out.println(1<<4);
System.out.println(1>>4);
System.out.println(5>>2);

16
0
1

Here I leave link where I tried them.

    
asked by Dacoso 16.09.2016 в 16:20
source

2 answers

3

They are bit-level operators

<<   desplazamiento a la izquierda, rellenando con ceros a la derecha
>>   desplazamiento a la derecha, rellenando con el bit de signo por la izquierda

Perform the manipulation of the bits of the data with which they operate. The data must be of the integer type.

  

1

answered by 16.09.2016 / 16:42
source
3
'>>': desplazamiento a la derecha de los bits del operando
'<<': desplazamiento a la izquierda de los bits de operando

< < "Offset to the left"

We wish to run number 33 two positions to the left. Then we made:

int j = 33;
int k = j << 2;

This is the result:

00000000000000000000000000100001 : j = 33
00000000000000000000000010000100 : k = 33 << 2 ; k = 132   

Each "hole" that is on the right after running this number is filled with zeros. The bits on the left are lost, it is not a rotation operation. If we pay attention, we will observe that this operation multiplied to j by 2 so many times as positions have been displaced. In this case it multiplied by 4 (2 x 2). Note that the number sign may change after the operation (for example 1

answered by 16.09.2016 в 16:44