Looking at the results, the JavaScript bitwise shift isn't. It's a division with rounding.
This is ugly, but does what you want:
sub rshift {int($_[0]/(1<<$_[1])+($_[0]<0?-0.5:0.5))}
for (2140837940,-1934825877,-2004116368,1978483312,-293893010) {
print "$_ >> 8 = ",rshift($_,8),"\n"
}
__END__
2140837940 >> 8 = 8362648
-1934825877 >> 8 = -7557914
-2004116368 >> 8 = -7828580
1978483312 >> 8 = 7728450
-293893010 >> 8 = -1148020
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|