In reviewing our number system, let’s look at what the number 1234 means to us. We are taught very early on about place values. In the decimal number system, starting at the rightmost digit we have the ones place, then the tens place, hundreds place, and finally the thousands place. We are only allowed the digits 0 through 9. The number 1234 represents the following components:
1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1
or
1 * 103 + 2 * 102 + 3 * 101 + 4 * 100
We are very accustomed to using decimal numbers so we do not think about breaking a number into its components. However, it is important to learn and understand what numbers represent, often by using hands-on activities.
Example: Given an integer variable num, reverse the number. If we knew there were three digits, we could
int a = num / 100;
int remainder = num % 100;
int b = remainder / 10;
int c = remainder % 10;
//now we could reassemble it
num = c * 100 + b * 10 + a;
This is similar to what we did in Lab Assignment A3.2, Coins.
If we do not know how many digits are in the number, we could use a loop.
int x = 12345;
int newX = 0;
int temp = 0;
while (x > 0) {
temp = x % 10;
newX = newX * 10 + temp;
x /= 10;
}
x = temp;
If we move to the right of the decimal place, we start using negative exponents.
103 | 102 | 101 | 100 | 10-1 | 10-2 |
Last modified: December 12, 2022
Back to Introduction To Number Systems