d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
yeah, I'm using two's compliment
0xFFFFFFFF (stored in binary bitstring)
should be -1, but it is showing up as 4294967295
You said you already tried the 'l' template and it gave you something wrong. On my computer it seems to work OK. Did you test it in something like the following way?
0xFFFFFFFF (stored in binary bitstring) would be a string of 32 1's, right?
#!/usr/bin/perl
use strict;
use warnings;
my $bitstring = '1' x 32; #String of 32 1's
my $pl = pack 'l', $bitstring; #Create a packed long integer
my ($num) = unpack 'l', $pl;
print $num; #Prints -1
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
After posting the above I tried substituting 'l' for 'N' in your original script and it also printed -1 for me, so I guess the problem is you are running it on a different platform. I'm using Ubuntu 10.04.3 LTS and my computer is 32-bit.
#!/usr/bin/perl
use strict;
use warnings;
print bin2dec('1' x 32); #Prints -1
sub bin2dec {
return unpack("l", pack("B32", substr("0" x 32 . shift, -32)));
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159