Member #167169

Member Since: December 1, 2010

Country: United States

  • Tutorial - Number System Conversions | about 3 years ago

    seanroberts:<br />
    <br />

    seanroberts: Why does it have to be 0b and 0x to tell which form its in, not b and x?<br />
    <br />

    I understand the need for a prefix,I just want to know why it has to be 0b or 0x instead for just b or x. You were talking about making things ambiguous, and having different types of numbers stat with 0 sounds ambiguous.<br />
    <br />
    More to your point, the reason is speed and convenience for a parser. When a computer program is trying to read text it helps to break things down into smaller pieces. If you were to write a compiler or parser or whatnot, you might start by breaking all the text down into words based on were you find blank space. With that done, your program may loop over every word trying to make sense of it. For each token (word) you need to decide whether it’s a number, punctuation, keyword, or other word. What’s a quick way for a program to know if a token is actually a number? It makes things easier if you make a rule that all numbers must start with a number, regardless of the number base (also called the radix). <br />
    <br />
    Have you noticed that many programmimg languages have a rule that function and variable names can have letters and numbers but they must not start with a number? It’s for the same reasons. That rule is the “other side of the coin” if you follow what I mean. <br />
    <br />
    Using 0 allows for quick determination that the token is a number and that the following character will tell you what kind of number. That’s why zero is used. It’s for speed.

No public wish lists :(