From "Mastering Perl/Chapter 16/Bit Operators/Unary NOT,~":
The unary NOT operator (sometimes called the complement operator), ~, returns the bitwise negation, or 1's complement, of the value, based on integer size of the architecture
Why does the following script output two different values?
#!/usr/local/bin/perl
use warnings;
use 5.012;
use Config;
my $int_size = $Config{intsize} * 8;
my $value = 0b1111_1111;
my $complement = ~ $value;
say length sprintf "%${int_size}b", $value;
say length sprintf "%${int_size}b", $complement;
Output:
32
64
The key phrase is "based on integer size of the architecture". You're running this on 64 bit architecture, so the complement will be returned as a 64 bit integer. Running the code on 32 bit architecture yields an output of 32 32
.
Using Devel::Peek::Dump
, you can see that $complement
is an unsigned integer whereas $value
is not. This piece of documentation indicates that unsigned integers are at least 32 bits, but platform-dependant. I can only assume they meant "depends on what a local C compiler calls 'long'" rather than int, as most compilers (GCC included) use 32 bit ints on 64 bit architecture.