Results 1 to 7 of 7

Thread: Hex is confusing

  1. #1
    Staifour
    Guest

    Hex is confusing

    i'm really confused
    when you use Hex with 65535 it'll return FFFF (that's no problem )
    but the problem is when you try with -1, it'll also return FFFF
    VB Code:
    1. Debug.Print Hex(-1)
    2. 'This will retrun FFFF
    3. Debug.Print Hex(65535)
    4. 'This will return FFFF too
    5. 'Or another way
    6. Debug.Print Hex(-1) = Hex(65535)
    7. 'this will return true
    8. 'i know this seems confusing (the =) but here the = is conditional
    9. 'it's not a = of a set statement (there is no problem with this = )
    what's the problem ???
    is it right mathematically ???
    well i think it isn't right mathematically cause from my experience maths are always logical , and this is not logical beacause when you try to do the conversion in the other direction (Hexadecimal --> Decimal ) you won't know whether the origin was 65535 or -1

    and another reson for findinf it not logical is that when using the classical way of converting any base to base 10 ( the number in the column from the right * the base ^ (The column number -1 ) + the same thing) you'll get that FFFF is 65535 not -1 but Val ("&HFFFF") = -1
    VB Code:
    1. 'the way of converting, it'll work with bases < 10
    2. dim TheLastNumber
    3. for i = 0 to len ( TheNumber) -1
    4.    TheLastNumber = TheLastNumber + (mid(TheNumber , len(theNumber) -i ,1) ^ i)
    5. next i
    Very confused

    BTW the same thing happens with OCT
    VB Code:
    1. Debug.print Oct(-1) = Oct(65535)
    2. 'this will return true

    any help ?????

  2. #2
    DaoK
    Guest
    because it unsigned and when you go in negative number it come back...see that drawing:


    Code:
    x|0----------| FFFFFFFF
    *x  = If you go here you will go at the end

  3. #3
    jim mcnamara
    Guest
    Hex is generally used to represent the bit pattern in variable or a cpu register.

    How you see the number the bit pattern represents depends on the kind of variable. Unsigned integer FFFF is 65335, signed is -1.

    VB always uses signed numbers. If you program in C, this signed- unsigned problem is an everyday thing.

  4. #4
    Staifour
    Guest
    i thought that that what was happening
    but since i'm programming my own Base converter i was wondering what is right mathematically
    for example is the Hexadecimal value of any minus number = - * Abs(the hexadecimal number )
    (i know that Abs doesn't deal with hexadecimal numbers and that you cannot multiply a hexadecimal number by -1 using * , but i don't mean the code i mean the way)
    or if it isn't that, what is the form of a hexadecimal minus number (let's say -15 is it -F ??? )

  5. #5
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Smile Info.

    A negative hexadecimal number, or any negative base number, is generally prefixed with a '-' symbol, so you are correct, -15 in hex would be -F.
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  6. #6
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    The same or related confusion has been the subject of a lot of threads.

    The confusion is due to ignoring word length (number or register length), which is critical to interpreting complement two binary arithmetic. Signed magnitude binary is easier to interpret but less efficient for a CPU.

    In VB, Integers are 16-bit (2-byte) variables, while Longs are 32-bit variables. Ignoring the order of bytes stored internally, the following are true.
    • FF FF (16 one bits) in an integer variable represents minus one.
    • 00 00 FF FF in a Long variable represents decimal 65, 535, because the sign bit (high order bit) is zero. The highest order one bit in this Long variable has a weight of 32,768 (decimal). If an output conversion routine or a hand calculator display does not show the high order Hex digits for this value, it can be confusing.
    • In an Integer variable the high order bit is the sign bit. It has a weight of -32,768 (decimal). The largest positive Integer is EFFF (zero bit followed by 15 one bits), which is 32,767 (decimal). If the high order bit is changed from zero to one, (weight -32,768), the result is 32,767 - 32,768 = minus one.
    • Note that 8000 (one bit followed by 15 zero bits) is -32,768 in an Integer, but is +32,768 in a Long.
    Confusion is often caused when conversion for output or a hand calculator display does not show leading zeros.

    BTW: To negate a 2-complement number, reverse each bit (zero becomes one and vice versa). Then add a low order one, using ordinary binary arithmetic. This algorithm is valid for changin a positive to a negative or vice versa. You must apply the algorithm to every bit of a register, including high order zero bits.

    Note that in a 2-complement system, overflow is recognized by the carry into the sign bit being different from the carry out of the sign bit. Positive overflow, is a carry of one into the sign and no carry out. Negative overflow is a carry out of the sign bit and no carry into the sign bit. When adding negative numbers there is usually a carry into the sign and a carry out.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  7. #7
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    The largest positive Integer is EFFF (zero bit followed by 15 one bits),
    I suspect you mean 7FFF, of course
    The (4 bit) binary value of 7 is 0111, while the binary value of E is 1110.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width