S
sonet
print (2 & (1 * (2**32)));
The result is 2 ??
The result is 2 ??
sonet said:print (2 & (1 * (2**32)));
The result is 2 ??
Paul Lalli said:You're likely to get a much more informative response if you explain
why you think it should be something other than 2.
Paul Lalli
Paul Lalli said:You're likely to get a much more informative response if you explain
why you think it should be something other than 2.
Sisyphus said:Admittedly it's not a very well phrased question - but it doesn't surprise
me that someone should find that result surprising.
'perldoc perlop' says that Binary "&" returns its operands ANDed together
bit by bit. On the basis of that, it's hard to see how
100000000000000000000000000000000
&
000000000000000000000000000000010
is going to return anything other than 0. I assume there's an explanation
somewhere in the perl docs - but I couldn't find it.
David said:sonet, mié20051228@11:06:40(CET):
Of course. '2 & anything' will yield 2 for any even 'anything', and 0 for
any odd 'anything'. See perlop.
Sisyphus said:Admittedly it's not a very well phrased question - but it doesn't surprise
me that someone should find that result surprising.
'perldoc perlop' says that Binary "&" returns its operands ANDed together
bit by bit. On the basis of that, it's hard to see how
100000000000000000000000000000000
&
000000000000000000000000000000010
is going to return anything other than 0. I assume there's an explanation
somewhere in the perl docs - but I couldn't find it.
Does this help explain?
$ perl -le'printf ("%b\n", 2**32)'
11111111111111111111111111111111
On a 32 bit system, the biggest number that can be expressed is 2**32
- 1. Your buffer doth flow over. ;-)
Eric said:I would have guessed that, with %b, you'd get a binary
representation of the floating-point number. Apparently, however, when you
try to %b (or %d) a number greater than 2**32-1, you get -1.
Perl must internally be detecting that the floating-point number is too
large, and substituting -1 in that case.
print (2 & (1 * (2**32)));
The result is 2 ??
Does this help explain?
$ perl -le'printf ("%b\n", 2**32)'
11111111111111111111111111111111
On a 32 bit system, the biggest number that can be expressed is 2**32 -
1. Your buffer doth flow over. ;-)
Does this help explain?
$ perl -le'printf ("%b\n", 2**32)'
11111111111111111111111111111111
On a 32 bit system, the biggest number that can be expressed is 2**32 -
1. Your buffer doth flow over. ;-)
Sisyphus said:You get the same for 2 ** 33 (and larger powers). Is this reliable, defined
behaviour ? Or is it machine dependent ? Or is it just plain undefined
behaviour ? I'm a little curious as to where the documentation is.
Yes - but shouldn't this be spelled out clearly in the perl docs ? (I would
think it *is* spelled out clearly in the perl documentation .... somewhere
.... though I can't find it .)
Why would the buffer overflow? I thought if you did arithmetic that was
too big to fit into an integer, perl automagically promoted the number to a
floating-point.
In perldoc perlnumber, it lists the six possible conversions (involving
to and from decimal strings, native integers, and native floats). I
believe the second rule listed applies:
"If the source number is outside of the limits representable in the
target form, a representation of the closest limit is used. (Loss of
information)"
The source number in this case (2 ** 33) is outside the limits
representable in the target form (native integer, as we specifically
requested an integer format in the sprintf9)), and so the
representation of the closest limit (2 ** 32 - 1) is used.
Sisyphus said:You get the same for 2 ** 33 (and larger powers). Is this reliable, defined
behaviour ?
Or is it machine dependent ?
That explains why 'printf("%b", 2 ** 32)' produces the output it does ...
but it's still not glaringly obvious (to me) that 2 & (2**32) is going to
produce '2'.
Why not? It's been shown that 2**32 is a string of 32 1's.
It has?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.