Jump to content

Right Shift int 1 with 32 bits


saviola

Recommended Posts

Hi all,

 

i'm  a little confused. I try to doing some bitwise manipulations, but came across the following strange phenomenon:

I try to do something like this :

$x = 1;
$x = $x >> 31;
echo $x * pow(2,4);

 The output is 0 

and

$x = 1;
$x = $x >> 33;
echo $x * pow(2,4);

 The output is 0 

but

$x = 1;
$x = $x >> 32;
echo $x * pow(2,4);

 The output is 16 

 

I am not very familiar with bit operations :-[, so can anyone help with some expolanation?

 

Thanks advance!

 

P.S. OS Win Xp 32bits , PHP Version 5.2.13

Link to comment
https://forums.phpfreaks.com/topic/210332-right-shift-int-1-with-32-bits/
Share on other sites

  • 3 months later...

It looks like the rightshift implementation mods the shift number by the number of bits your system is, in your case 32.

 

At the command line on a 64-bit system; rightshifting 1 and 8 from 0 to 66:

$ php -r 'foreach(range(0,66) as $n) printf("%2d (%2d): %064b\n", $n, $n % 64, 1 >> $n);'
0 ( 0): 0000000000000000000000000000000000000000000000000000000000000001
1 ( 1): 0000000000000000000000000000000000000000000000000000000000000000
2 ( 2): 0000000000000000000000000000000000000000000000000000000000000000
3 ( 3): 0000000000000000000000000000000000000000000000000000000000000000
4 ( 4): 0000000000000000000000000000000000000000000000000000000000000000
5 ( 5): 0000000000000000000000000000000000000000000000000000000000000000
6 ( 6): 0000000000000000000000000000000000000000000000000000000000000000
7 ( 7): 0000000000000000000000000000000000000000000000000000000000000000
8 ( : 0000000000000000000000000000000000000000000000000000000000000000
9 ( 9): 0000000000000000000000000000000000000000000000000000000000000000
10 (10): 0000000000000000000000000000000000000000000000000000000000000000
11 (11): 0000000000000000000000000000000000000000000000000000000000000000
...snip...
59 (59): 0000000000000000000000000000000000000000000000000000000000000000
60 (60): 0000000000000000000000000000000000000000000000000000000000000000
61 (61): 0000000000000000000000000000000000000000000000000000000000000000
62 (62): 0000000000000000000000000000000000000000000000000000000000000000
63 (63): 0000000000000000000000000000000000000000000000000000000000000000
64 ( 0): 0000000000000000000000000000000000000000000000000000000000000001
65 ( 1): 0000000000000000000000000000000000000000000000000000000000000000
66 ( 2): 0000000000000000000000000000000000000000000000000000000000000000

$ php -r 'foreach(range(0,66) as $n) printf("%2d (%2d): %064b\n", $n, $n % 64, 8 >> $n);'
0 ( 0): 0000000000000000000000000000000000000000000000000000000000001000
1 ( 1): 0000000000000000000000000000000000000000000000000000000000000100
2 ( 2): 0000000000000000000000000000000000000000000000000000000000000010
3 ( 3): 0000000000000000000000000000000000000000000000000000000000000001
4 ( 4): 0000000000000000000000000000000000000000000000000000000000000000
5 ( 5): 0000000000000000000000000000000000000000000000000000000000000000
6 ( 6): 0000000000000000000000000000000000000000000000000000000000000000
7 ( 7): 0000000000000000000000000000000000000000000000000000000000000000
8 ( : 0000000000000000000000000000000000000000000000000000000000000000
9 ( 9): 0000000000000000000000000000000000000000000000000000000000000000
10 (10): 0000000000000000000000000000000000000000000000000000000000000000
11 (11): 0000000000000000000000000000000000000000000000000000000000000000
...snip...
59 (59): 0000000000000000000000000000000000000000000000000000000000000000
60 (60): 0000000000000000000000000000000000000000000000000000000000000000
61 (61): 0000000000000000000000000000000000000000000000000000000000000000
62 (62): 0000000000000000000000000000000000000000000000000000000000000000
63 (63): 0000000000000000000000000000000000000000000000000000000000000000
64 ( 0): 0000000000000000000000000000000000000000000000000000000000001000
65 ( 1): 0000000000000000000000000000000000000000000000000000000000000100
66 ( 2): 0000000000000000000000000000000000000000000000000000000000000010

 

You'll notice that at 64, the results start again as they would have from the beginning.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.