saviola Posted August 10, 2010 Share Posted August 10, 2010 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 Quote Link to comment Share on other sites More sharing options...
Wildbug Posted November 19, 2010 Share Posted November 19, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.