Unary NOT/Integersize of the architecture

Posted by sid_com on Stack Overflow See other posts from Stack Overflow or by sid_com
Published on 2010-12-31T07:49:07Z Indexed on 2010/12/31 7:53 UTC
Read the original article Hit count: 295

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

© Stack Overflow or respective owner

Related posts about perl

Related posts about int