On Mon, Jan 13, 2020 at 11:30 PM ToddAndMargo via perl6-users <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?

Reply via email to