Re: ones and twos compliment

[email protected] (Paul Procacci) Sat, 7 Jun 2025 13:22:23 -0400
Newsgroups perl.perl6.users
Message-ID <CAFbbPugopnX5qNJT34EA4sUaEzd=-DO5UQkLefBtjOS6EEpWrA@mail.gmail.com>
my $number = 42;
my $ones-complement = +^$number;
my $twos-complement = +^$number + 1;

~Paul

On Sat, Jun 7, 2025 at 11:26 AM Will Coleda <[email protected]> wrote:
>
> (You may have to manually paste that URL, gmail cut off the trailing ^ here when I clicked on my own link)
>
> On Sat, Jun 7, 2025 at 4:50 PM Will Coleda <[email protected]> wrote:
>>
>> Two's complement (from google search of the docs site): https://docs.raku.org/language/operators#prefix_+^
>>
>>
>>
>> On Sat, Jun 7, 2025 at 4:53 AM ToddAndMargo via perl6-users <[email protected]> wrote:
>>>
>>> Hi All,
>>>
>>> Is there an easy way to print ones' and two's compliment
>>> of a 32 bit integer?
>>>
>>> Many thanks,
>>> -T
>>>
>>>
>>> sub ones-complement(Int $num) {
>>>      my $binary = $num.base(2);
>>>      my $complement = '';
>>>      for $binary.comb -> $bit {
>>>          $complement ~= $bit eq '0' ?? '1' !! '0';
>>>      }
>>>      return $complement;
>>> }
>>>
>>> my $number = 5;
>>> my $complement = ones-complement($number);
>>> print "Ones complement of $number is $complement\n";
>>>
>>>
>>> seems a bit much:
>>> https://search.brave.com/search?q=raku+print+ones+compliment&source=web&summary=1&conversation=00fda578d0badafdf15143



-- 
__________________

:(){ :|:& };: