http://qs1969.pair.com?node_id=11108838


in reply to Re^5: Can I access and use UV types from perl?
in thread Can I access and use UV types from perl?

Yes the image helped, I can see from that that perhaps this is what happens. To calculate a two's complement number take the difference of the highbit masked number, with the complement mask of the highbit number. The number being negative when the highbit is set, essentially saying complement mask minus the highbit mask number.

We could use pairs of numbers, such that the 'highbit' could be any binary number and the difference found between that and its pair then resolves into an integer. It's a different way of storing and operating on the numbers.

update added example code

=head1 polynumber binary pairs (bifields?) 0 00_00 -1 1 00_01 01_00 -2 2 00_10 10_00 -3 3 00_11 11_00 =cut

But, we get overlaps.

=head1 polynumber binary pairs (bifields?) overlaps 1 -2 10_01 01_10 -1 1 01_10 10_01 -3 0 00_11 11_11 =cut

note: row polynumbers are read opposite significant highbit first. That is, if these are 'N'etwork order they should be read 'V'ax order. This illustrates the concept.

edit added whitespace, and note

Replies are listed 'Best First'.
Re^7: Can I access and use UV types from perl?
by haukex (Archbishop) on Nov 17, 2019 at 23:39 UTC

    Sorry, I'm afraid I don't follow... but since you mention the high bit, note how in two's complement, the high bit is basically the sign bit. And the other nice thing is that the binary math still works well:

    • -8+1 is 1000+0001, which =1001, which is -7
    • -2-2 is 1110+1110, which =11100, drop the overflow and 1100 is -4
    • -2+5 is 1110+0101, which =10011, drop the overflow and 0011 is 3

    Of course, there are still overflow issues, e.g. 7+1 turns out as -8, but those will always happen with any fixed number of bits. Of course, the advantage of Perl's scalars here is that they will upgrade themselves automatically! (At the risk of losing some precision)

      ohhhh whaaat, I absolutely had my answer on preview for this one and then I knocked my pc off luls...

      in essence, I was looking at the integers -8 .. 7 were constructed from the 4 bits rather than how the operation of additon occured upon them.

      However, the outcome is that integers as we see them are of two subsets.

      The signed integers are composed of a full mask split into a significant bit mask and the complement, the integer being derived from the difference of the component mask to the highbit mask.

      The unsigned integers are composed of a full mask split into two, a full mask and its complement (a no-mask), the integer being derived from the differnce of the full mask to the complement mask.

      Then there are the unused integers, of the type displayed in the example above Re^6: Can I access and use UV types from perl?. That is where the masks are of a size such that the mask and its complement fills the size of the data providing the integer, and not being 1 or 0 on either side. For example, equal sizes.

      An example of the masking to produce integers.

      complement mask - sigbitmask + complement mask - sigbitmask 0111 - 1000 + 0111 - 1000 c) 1110 + 0101 0110 - 1000 + 0101 - 0000 (6) - (8) + (5) - (0) (-2) + (5) = 3

      Example c number used, taken from Re^7: Can I access and use UV types from perl?, but this works with the others too.

      So integers are what we think they are, the difference of pairs of natural numbers.

      edit added link to reply containing number used in this example, courtesy haukex
        You seem to be over-complicating things. In a computer, registers and memory words hold a collection of bits (e.g. 64 bits). How we interpret those bits is up to the programmer and/or the CPU's ALU. One common way is to treat the bit pattern as as an unsigned binary number. Another common way is to treat them as a two's complement signed value - where values with the high bit set are treated as negative numbers from the point of view of comparisons etc. Other possibilities in the past have included just using one bit as a sign bit, and using one's complement.

        The big advantage of two's complement as a way of representing negative numbers is that they can be added and subtracted exactly the same way as unsigned numbers, the only differences being when the overflow flag on the ALU should be set.

        But we now seem to be a long way off from anything that has to do with perl.

        Dave.