awohld:

Just to amplify a little on Util's response: A number is just a number and decimal, hex, octal, etc., are just different ways to represent it. To the computer it's just a bunch of bits.

So, why have different representations? Some operations are easier to visualize and check in a different base. For example, I've done a boatload of assembly programming, so I would use hex frequently. One example: If you want to represent the ASCII code for the numbers 0..9 you could use decimal (48=='0', 49=='1', ... 57=='9'), octal (60=='0', 61=='1', ... 71=='9'), hex (30=='0', 31=='1', ... 39='9'), or any other base. But I would use hex because it's easy to remember "just add the digit I want to 30 to get the ASCII code for the digit".

Another example: The same rule generally applies to the alphabet (0x41=='A', 0x42=='B', ...) which makes it fairly easy to convert from a letter to an ASCII code in decimal. And converting from UPPER CASE to lower case is easy, just OR the letter with 0x20 (0x61=='a', 0x62=='b', ...)

If you're programming peripheral chips, such as UARTs or such, you'll find that many of them map specific functions to particular bits in a specific memory or I/O location. For example, the fictional 3141 DWIM chip performs various duties based on the value you write to 0x2718:

bit 7: Take over the world bit 6: Turn on your porch light bit 5: Change your socks bit 4: Format your hard disk bit 3: Sell Z-80 computer on Ebay "No Reserve L@@K! RARE!" bit 2: Apply lipstick to pig bit 1: Make horse drink bit 0: Lead horse to water
So if you write 57 decimal to that location, it's hard to tell what will happen just by looking at the number. Is your precious Z-80 computer safe? Will you take over the world? Will you change your socks? (Hey, it's not Tuesday!)

But if you use binary, it's much simpler to tell. Let's see 57 converts to 0011 1001. Drat! Not only do I not get to control the world, but I've left my porch light off! Oh, no! My precious Z-80! Hmmmm.... I've lead the horse to water, but I forgot to make him drink!

</foolish_mode>

--roboticus


In reply to Silly Example (was Re: Hex Numbers and Range Operator) by roboticus
in thread Hex Numbers and Range Operator by awohld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.