use strict ; use warnings ; use Math::BigInt ; my $hex = '-a' ; my $sign = '0' ; if( $hex =~ /^-/ ) { $hex =~ s/^-//g ; $sign = 1 ; } my $big_int = Math::BigInt->new( '0x'.$hex ) ; my $binary = $big_int->as_bin() ; my $two_comp = $binary ; $two_comp =~ s/^0b/$sign/; print "$two_comp\n";