Bitwise Right operator [duplicate]

0

Hello

By doing some studies on PHP I came across bitwise and until then all its operations have not been anything difficult, just the right shift

An example that I picked up and still did not understand was this:

print(4>>6); //onde o resultado final é igual a zero

And when I looked at a book I realized the following calculation for this expression:

4/2^6
2^6 = 2*2*2*2*2*2=64
64/4 = 0,06 

That in the case would be zero, which is the result that php returns me, although I think it should give 16. Could someone explain this logic to me?

    
asked by anonymous 05.08.2016 / 01:22

2 answers

2

See 4 in base 2:

00000100

Now let's move once to the right ( 4 >> 1 ):

00000010

If we travel twice (% with%):

00000001

Moving 6 houses, the bit was "though", so the result 4 >> 2 .

See the left shift, I'll get the number 6 as an example, and move 2 times:

00000110 (6)

00011000 (6<<2 = 24)

In summary:

  • When you move to right , you are dividing by 2 with each step.

  • When you move to left , you are multiplying by 2 with each step.


See more details here:

How does bit-shift work in C / C ++?

    
05.08.2016 / 02:31
2

The dividend is 4 and the divisor is 64.

The expression is inverted.

The right then is 4/64, whose result is 0.0625

    
05.08.2016 / 02:21