Re: ones and twos compliment

[email protected] (ToddAndMargo via perl6-users) Sat, 7 Jun 2025 12:12:26 -0700
Newsgroups perl.perl6.users
Message-ID <[email protected]>
>>> On Sat, Jun 7, 2025 at 4:53 AM ToddAndMargo via perl6-users <[email protected]> wrote:
>>>> Is there an easy way to print ones' and two's compliment
>>>> of a 32 bit integer?
>>>>
>>>> Many thanks, -T

On 6/7/25 10:22 AM, Paul Procacci wrote:
 > my $number = 42;
 > my $ones-complement = +^$number;
 > my $twos-complement = +^$number + 1;
 >
 > ~Paul
 >

First attempt:

$ repl
Welcome to Rakudo™ v2024.07.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2024.07.

[0] > my int32 $y=-1073741510
-1073741510

[1] > my $ones-complement = +^$y;
Bytecode validation error at offset 164, instruction 23:
operand type 32 does not match register type 24 for op getlex_ni in 
frame <unit>

[1] > my $twos-complement = +^$y + 1;
Bytecode validation error at offset 164, instruction 23:
operand type 32 does not match register type 24 for op getlex_ni in 
frame <unit>

No idea where to report this to.


Second attempt:

$ p6 'my int32 $y=-1073741510; my $ones-complement = +^$y; my 
$twos-complement = +^$y + 1; print "\ny               = 
<$y>\nones-complement = <$ones-complement>\ntwos-complement = 
<$twos-complement>\n\n";'

y               = <-1073741510>
ones-complement = <1073741509>
twos-complement = <1073741510>


Thank you!
-T