in reply to Re: Bit fiddling madness
in thread Bit fiddling madness
++sn1987a (when the Vote Fairy next visits) for an ingenious observation! Unfortunately, I don’t think it answers the OP’s question, as masking with bitwise-AND does remove the sign bit, even with use integer in effect:
#! perl use strict; use warnings; print "\nno integer\n"; my $bit = -1 >> 31; printf "unmasked: 0x%x = %d\n", $bit, $bit; $bit = (-1 & 0x80000000) >> 31; printf "masked: 0x%x = %d\n", $bit, $bit; use integer; print "\nuse integer\n"; $bit = -1 >> 31; printf "unmasked: 0x%x = %d\n", $bit, $bit; $bit = (-1 & 0x80000000) >> 31; printf "masked: 0x%x = %d\n", $bit, $bit;
Output:
1:29 >perl 868_SoPW.pl no integer unmasked: 0x1ffffffff = 8589934591 masked: 0x1 = 1 use integer unmasked: 0xffffffffffffffff = -1 masked: 0x1 = 1 1:29 >perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x +86-multi-thread-64int
:-(
Update: As BrowserUk observes below, masking removes the sign bit (in this case) only in perls built with 64-bit ints. For perls built with 32-bit ints, sn1987a’s observation does answer the OP’s question. (Moral for self: test, test, test!)
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Bit fiddling madness
by BrowserUk (Patriarch) on Feb 11, 2014 at 16:06 UTC | |
by mbethke (Hermit) on Feb 11, 2014 at 16:36 UTC | |
|
Re^3: Bit fiddling madness
by sn1987a (Curate) on Feb 11, 2014 at 16:07 UTC |