Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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
like image 250
sid_com Avatar asked Nov 28 '25 21:11

sid_com


1 Answers

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.

like image 75
ysth Avatar answered Dec 01 '25 18:12

ysth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!