Shop OBEX P1 Docs P2 Docs Learn Events
Hex or Dec? — Parallax Forums

Hex or Dec?

MichelBMichelB Posts: 154
edited 2009-10-11 16:17 in BASIC Stamp
Hi Specialists in Code, in the BASIC Stamp Syntax and Reference Manual 2.2, page 112, it's written: "It may be clearer to express the */ multiplier in hex - as $0180 - since hex keeps the contents of the upper and lower bytes separate. Here's an example:"...
If you replace in the example $0180 (that keeps the contents of the upper and lower bytes separate) by the Dec value i.e. 384 the result is the same. Where is the advantage of "hex" explained above? Thank you in advance.
·

Comments

  • Marz KrishnaMarz Krishna Posts: 26
    edited 2009-10-10 17:03
    the advantage is because you can't type 1.5 into a pbasic expression. the pbasic compiler cannot understand 1.5. it only understands variableX.highbyte if you type a period unless the period is in a "string". the $0180 tells the */ operand and pbasic that you want to multiply by 1.5. otherwise you would just get a compile error because the pbasic parser/compiler would assume you're trying to do a varX.highbyt or something with incorrect syntax. the other advantage is that using hex let's you see clearly what's in what byte in memory. decimal doesnt care about byte boundaries. Hex is designed to make memory maps easier to read and digital data (base 2 number system, eg 2^8=256, 8 bits = 1 byte(256 values). so you can see every byte in memory as a single character eg. 'F', as opposed to two character eg. '16'. so if you look at your memory map, instead of seeing 169138, you would see f9d8. in the case of f9d8 you know you have 4 bytes just by looking at it, but in the case of 169138 you could possibly be confused and think you have 1, 69,138 etc. so in the case of $0180 you know that '0' is a byte, 1 is a byte, etc.

    Post Edited (Marz Krishna) : 10/10/2009 8:24:14 PM GMT
  • SRLMSRLM Posts: 5,045
    edited 2009-10-10 17:38
    You're missing a part of the manual half a paragraph earlier: "The whole number is the upper byte of the multiplier (0 to 255 whole units) and the fraction is the lower byte of the multiplier (0 to 255 of 1/256 each)."

    So if you put it into dec, you would have to convert to base 2 (or power of 2) to find out which are the upper and which is the lower bytes, then convert each back into dec to get the multiplier. This way, it just keeps a step out.
  • MichelBMichelB Posts: 154
    edited 2009-10-11 11:37
    Thank you SLRM, OK for the step out.
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-10-11 16:17
    What ought to be emphasized is that decimal, hexadecimal, binary, octal etc. are not different ways for the machine to represent a number. The machine represents the number the same way regardless.

    Hexadecimal, decimal, octal, binary, are different ways for you to represent numbers IN YOUR MIND. Each has advantages and disadvantages. Hexadecimal makes it easier for you to see quickly the contents of the upper and lower nibbles of a byte. Decimal makes it easier to do arithmetic in your head, because you're used to it. Binary makes it easy for you to think in terms of individual bits.

    The machine doesn't care. It goes along blindly, doing whatever you tell it to do, never adding any insights of its own, not knowing what it does, not caring -- just like Democrats.

    -- Carl

    P.S. that last line is mostly stolen from the Bob Hope movie "Ghost Busters".
    .

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
Sign In or Register to comment.