hex equivalent of negative numbers

Issue #506 invalid
Former user created an issue

Originally reported on Google Code with ID 506

Steps to reproduce:
hex(-1)
= -0x1

Expected behavior:
It would be very nice if this would show the 2's compliment of any negative numbers.
 So, -1 would yield 0xFFFFFFFF (or 0xFFFFFFFFFFFFFFFF if 64-bit math is being used).

Experienced behavior:

Product version:
0.11

Operating system:
Windows 7 x64 Professional

Additional Information:
For an example of this, switch to "Programmer" mode in the built-in windows calculator.
 Set the radix to decimal, enter -1, then switch the radix to hex.

Reported by nhoch@specializedsolutionsllc.com on 2014-05-29 12:28:21

Comments (5)

  1. Former user Account Deleted

    Reported by helder.pereira.correia on 2014-12-17 21:45:01 - Labels added: Type-Enhancement

  2. Former user Account Deleted
    Actually, that's (a side-effect of) what the 'mask' function does. As in,
    
    hex(mask(-1; 32)) == 0xFFFFFFFF
    
    and
    
    unmask(0xFFFFFFFF; 32) == -1
    
    I don't think this could be implemented in the 'hex' function because it wouldn't know
    the desired number of bits. I feel the correct solution here would be "document it"...
    

    Reported by fkrull.0 on 2015-01-17 16:37:17

  3. Former user Account Deleted
    @fkrull: indeed. @nhoch: I don't know the windows calculator but I imagine it uses the
    CPU directly instead of an arbitrary precision math engine like SpeedCrunch. Hence
    the 0xFFFFFFFF or 0xFFFFFFFFFFFFFFFF results.
    

    Reported by helder.pereira.correia on 2015-01-19 13:45:49 - Status changed: Rejected

  4. Vedran Grudenic

    Note that it's rather easy to define a user function which does this, using something like:

    shex(x; bits) = hex(mask(x; bits))   ? "signed hex"
    

    Or, you can create several functions for common word sizes:

    shex16(x) = hex(mask(x; 16))  ? dec -> hex 16-bit signed
    shex32(x) = hex(mask(x; 32))  ? dec -> hex 32-bit signed
    ...
    sdec16(x) = unmask(x; 16)  ? hex -> dec 16-bit signed
    sdec32(x) = unmask(x; 32)  ? hex -> dec 32-bit signed
    ...
    

    To use the function, just start typing "shex" and you're set.

  5. Log in to comment