I have problem when try to shift bit for a negative number with Perl.
For example:
printf("%d", (-10) >> 2);
This method print: 1073741821
With same condition, when I try to do it with Javascript:
document.write('test: ' + (-10 >> 2) + '<br>');
The output is -3
I would like to understand the different between Perl and Javascripts. Does Perl have problem with shifting bit for a negative number?
Thank you very much.
Perl uses unsigned binary right shift. If Perl runs on a 32-bit architecture, you get the following:
Unsigned binary right shift means that the highest (rigtmost) bits of the result become 0.
JavaScript uses sign-extending binary right shift. JavaScript shifts always treat both the input and the output as 32 bit integers. So you get:
Sign-extending binary right shift means that the highest (rigtmost) bits of the result get copied from the highest bit of the original value.
As Tim Henigan said in his answer, it’s possible to get sign-extending binary right shift from Perl, by specifying
use integer;. Example: