develooper Front page | perl.perl5.porters | Postings from November 2003

& and % aren't doing the same at U32_MAX

From:
Stas Bekman
Date:
November 4, 2003 15:15
Subject:
& and % aren't doing the same at U32_MAX
Message ID:
3FA83338.1020908@stason.org
Tels and I were wondering about this behavior of % and & when U32_MAX 
(4294967295) is crossed.

perl -le '$a = 2**32-5; printf "%2d: %10s %10s\n", $_, ($a+$_) % 2**32, 
($a+$_) & 2**32-1 for 1..10'
  1: 4294967292 4294967292
  2: 4294967293 4294967293
  3: 4294967294 4294967294
  4: 4294967295 4294967295
  5:          0 4294967295
  6:          1 4294967295
  7:          2 4294967295
  8:          3 4294967295
  9:          4 4294967295
10:          5 4294967295

If I understand correctly, $a+$_ moves from IV to NV when $_==5, but why does 
it affect & and not %. Should the docs have a clause describing which ranges & 
and % ops are valid at? How this is all going to change with U64?

Unrelated to the question of why there are different, just wanted to point out 
that 5.005_03 gave an even different view:

perl-5.005_03  -le '$a = 2**32-5; printf "%2d: %10s %10s\n", $_, ($a+$_) % 
2**32,($a+$_) & 2**32-1 for 1..10'
  1: 4294967292 4294967292
  2: 4294967293 4294967293
  3: 4294967294 4294967294
  4:          0 4294967295
  5:          0 4294967295
  6:          0 4294967295
  7:          0 4294967295
  8:          0 4294967295
  9:          0 4294967295
10:          0 4294967295


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About