Unary NOT / Architecture Integration

From "Mastering Perl / Chapter 16 / Bit Operators / Unary NOT, ~":

The unary NOT operator (sometimes called the complement operator), ~, returns a bitwise negation or 1 addition to the value based on the integer size of the architecture

Why are two different values ​​displayed in the following script?

#!/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;

Conclusion:

32  
64
+3
source share
2 answers

The key phrase is "based on the integer size of the architecture." You use this in a 64-bit architecture, so the complement will be returned as a 64-bit integer. Running code on a 32-bit architecture gives output 32 32.

Devel::Peek::Dump, , $complement , $value . , 32 , . , " , C " long ", int, ( GCC) 32- int 64- .

+2

intsize C 'int', " " , Perl , , . $Config{'ivsize'} ( uvsize; ), , ( ivtype/uvtype, , C).

, intsize 4, ivsize 8. %32b, 32; $value 8 , 32, $supplement 64 .

+3

Source: https://habr.com/ru/post/1782891/


All Articles