开发者

Unary NOT/Integersize of the architecture

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 followi开发者_开发知识库ng 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


intsize gives the width of a C 'int', but "the integer size of the architecture" means the size of integer that Perl actually will use, which can depend on how it was configured. Use $Config{'ivsize'} (or uvsize; the two ought to be the same) to see how long that is (or ivtype/uvtype to see what the C type is).

Where you are seeing a difference between the two lengths, intsize is 4, but ivsize is 8. You are using the format %32b which sets a minimum output width of 32; $value only uses 8 characters so is padded out to 32, but $complement uses 64 characters.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜