Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Number Bases Explained: Real-World Examples and Practical Algorithms

Notes 1

1. Real-World Analogies for Place Value

1.1 Apple Shipping Scenario

Suppose Alex has 123 apples. Each crate holds exactly 10 apples, and each truck holds exactly 10 crates. How many full trucks, crates, and leftover apples will there be? Most people can instantly answer: 1 truck, 2 crates, 3 leftover apples. The step-by-step calculasion is: 123 apples ÷ 10 = 12 full crates, remainder 3 apples. 12 crates ÷ 10 = 1 full truck, remainder 2 crates. Alternatively, you can directly map the digits of 123: the hundreds digit (1) is the number of trucks, tens digit (2) is crates, units digit (3) is leftover apples. This is the core meaning of decimal place value—each digit’s position represents a multiple of 10, with each "group of 10" being a carry operation (crating apples, loading crates on to trucks).

1.2 Currency Conversion Scenario

Alex has 123 1-cent coins and wants to exchange them for larger denominations. The result is 1 dollar, 2 dimes, and 3 cents. Again, the digits of 123 align with the denominations: hundreds digit equals dollars, tens digit equals dimes, units digit equals cents. This reinforces how place values translate directly to real-world quantities.

2. Place Value Fundamentals and Algorithms

2.1 Universal Place Value Formula

Any number in base b can be expressed as: $$\sum_{n=0}^{k} d_n \times b^n$$ Where:

  • $d_n$ is the digit at position n (starting from 0 for the rightmost digit)
  • $b$ is the base (10 for decimal, 16 for hexadecimal, etc.) For decimal numbers, this simplifies to summing each digit multiplied by 10 raised to its position index.

2.2 Programmatically Extracting Place Values

Computers cannot directly interpret digit positions like humans do. Instead, they use modulo and integer division operations to extract each "group count" (e.g., trucks, crates, apples):

  1. Use number % base to get the remainder, which is the value of the current least significant position.
  2. Use number = number / base (integer division, discarding remainders) to shift to the next higher place value.
  3. Repeat until the number becomes zero.

This process is equivalent to a right shift operation in decimal: dividing by 10 shifts all digits one position to the right, discarding the least significant digit (which is captured via the modulo operation).

2.3 Example: Apple Distribution Calculation

The following C++ funtcion calculates the number of trucks, crates, and leftover apples using this algorithm:

void calculateAppleDistribution(unsigned int totalApples) {
    unsigned int quantities[3] = {0};
    int index = 0;
    unsigned int remaining = totalApples;

    while (remaining > 0) {
        quantities[index] = remaining % 10;
        remaining /= 10;
        index++;
    }

    printf("Distribution: %d trucks, %d crates, %d leftover apples\n",
           quantities[2], quantities[1], quantities[0]);
}

2.4 Example: Convert Decimal Number to String

The same algorithm can be used to convert a decimal number to its string representation. Since we extract digits from least to most significant, we need to reverse the result at the end:

int decimalToString(unsigned int num, char* outputStr) {
    int length = 0;
    unsigned int remaining = num;

    while (remaining > 0) {
        outputStr[length] = (remaining % 10) + '0';
        remaining /= 10;
        length++;
    }

    // Reverse the string to get the correct order
    for (int i = 0; i < length / 2; i++) {
        char temp = outputStr[i];
        outputStr[i] = outputStr[length - 1 - i];
        outputStr[length - 1 - i] = temp;
    }

    outputStr[length] = '\0';
    return length - 1;
}

3. Hexadecimal Base (Base 16)

3.1 Adjusted Apple Shipping Scenario

Suppose Alex upgrades to larger containers: each crate holds 16 apples, and each truck holds 16 crates. For 123 apples, how many full trucks, crates, and leftover apples are there? Calculation steps: 123 apples ÷ 16 =7 full crates, remainder 11 apples (represented as 'B' in hexadecimal). 7 crates ÷16=0 full trucks, remainder7 crates. The result is 0 trucks,7 crates, 11 leftover apples, which is written as the hexadecimal number "7B". Here, each digit represents a multiple of 16: the rightmost digit is apples, next is crates, then trucks.

3.2 Example: Convert Decimal to Hexadecimal String

The following function extends the earlier algorithm to convert decimal numbers to hexadecimal strings, handling digits 10-15 as 'A'-'F':

int decimalToHex(unsigned int num, char* hexStr) {
    int length = 0;
    unsigned int remaining = num;

    while (remaining > 0) {
        unsigned char digit = remaining %16;
        if (digit >9) {
            hexStr[length] = 'A' + (digit -10);
        } else {
            hexStr[length] = '0' + digit;
        }
        remaining /=16;
        length++;
    }

    // Reverse to get correct hexadecimal order
    for(int i=0; i < length/2; i++) {
        char temp = hexStr[i];
        hexStr[i] = hexStr[length -1 -i];
        hexStr[length -1 -i] = temp;
    }

    hexStr[length] = '\0';
    return length -1;
}

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.