On Tue, Jan 14, 2020 at 7:45 AM Paul Procacci <pproca...@gmail.com <mailto:pproca...@gmail.com>> wrote:

     >> What is the syntax for a twos complement anyway?

    I'm not sure I understand the question.
    Two's compliment is +^ ... the routine you've been using.

    On Tue, Jan 14, 2020 at 12:33 AM ToddAndMargo via perl6-users
    <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:

         >> On Mon, Jan 13, 2020 at 11:30 PM ToddAndMargo via perl6-users
         >> <perl6-us...@perl.org <mailto:perl6-us...@perl.org>
        <mailto:perl6-us...@perl.org <mailto:perl6-us...@perl.org>>> wrote:
         >>
         >>     Hi All,
         >>
         >>     This works,
         >>
         >>          $ p6 'my uint8 $c = 0xA5; my uint8 $d =  +^$c; say
        $d.base(16);'
         >>          5A
         >>
         >>     But this does not:
         >>
         >>          $ p6 'my uint8 $c = 0xA5; say (+^$c).base(16);'
         >>          -A6
         >>
         >>     1) who turned it into an negative integer?
         >>
         >>     2) how do I turn it back?
         >>
         >>     Many thanks,
         >>     -T

        On 2020-01-13 21:18, Paul Procacci wrote:
         > If you read the signature for +^, you'll notice it returns an
        Int.
         >
         > In your first working example, you're taking a uint8 with
        binary value
         > 10100101, zero extending it to 64 bits via +^, applying a two's
         > compliment, and then assigning bits [0:7] to another uint8
        which at that
         > point contains the binary value of 01011010 (or hex value 0x5A).
         > In your second example that isn't working, you're taking
        uint8 with
         > binary value 10100101, zero extending it to 64 bits via +^,
        applying a
         > two's compliment, and then displaying this *Int* (64 bits) as
        hex.[1]
         > To turn it back you need to mask off bits [8:63] with: say
        ((+^$e) +&
         > 0x0FF).base(16);" [2]
         >
         > [1] I'd show you the 64 bit value but it's a bunch of 1's
        followed by
         > the value -0xA6.
         > [2] Note, since the type has been promoted to an Int there'
        no going
         > back to uint8 without an explicit assignment (afaik)
         >

        That explains it.  Thank you.

        I used uint8 to keep the ones to a mild torrent!

        If I am remembering correctly, 0xA5 going to 0x5A is
        a ones compliment.

        What is the syntax for a twos complement anyway?




On 2020-01-14 05:14, Gerard ONeill wrote:
A negative number (-A5) is the twos compliment of the positive number. A ones compliment is all the bits flipped.  A twos compliment is a ones compliment plus one.  So a ones compliment of (A5) is (-A5 - 1), which is -A6.

So presumably, the twos compliment operator is (-). And I suppose for consistency, +-A5 gives you -A5, which makes +- the twos compliment bitwise operator..


It is a pain to keep the variables from being "Coerced to Int",
but this pretty much shows it

$ p6 'my byte $x = 0xA5; say $x.base(16), " original";
      my byte $y = +^$x; say $y.base(16), " ones compliment";
      my byte $z = $x + $y + 1;
      say $z.base(16), "  original + 2s compliment";'

A5 original
5A ones compliment
0  original + 2s compliment

Reply via email to