It's nearly April and I'm in the midst of taking two computer science courses: Harvard's CS50x Introduction to Computer Science offered through Edx.org and Stanford's CS106A Programming Methodology offered through Stanford Engineering Everywhere (SEE, see.stanford.edu). The incredible fact that anyone can take these for free and at their own pace deserves its own blog post, but I want to briefly talk about some of the numerical systems I've seen in these courses that have sometimes led to confusion.
Numeral systems operate through the exponentiation of a constant base number for each positional digit in a number. For example, the decimal system is a base-10 system. If we were to take the decimal number 179, we would see that 9 is the one's position, 7 is the ten's position, and 1 is the hundred's position. In order to arrive at the number 179 in a base-10 numeral system, you would do the following:
179 = (9 * 10^0) + (7 * 10^1) + (1 * 10^2) = 9 + 70 + 100 = 179
Now lets try representing this number in binary. Binary is a base-2 numeral system, with positional digits having the values either 0 or 1. Each positional digit represent what is known as a bit (a contraction of the term binary digit), which is the smallest representation of data in a computer's memory. A series of 8 bits makes up a byte, and collections of bytes in turn are the building blocks of primitive data types such as characters or integers (both of which can be seen to be used in this blog post). So if we were to represent 179 in binary, it would go as follows:
179 = 10110011 = (1*128) + (0*64)+ (1*32)+ (1*16)+ (0*8)+ (0*4)+ (1*2) + (1*1) = 179
Did you see what happened there? The right-most digit of the binary number is the one's position, and each digit to the left represents a doubling of the digit's significance. 128 is the largest number to factor into 179, so we place a 1 in that position. We place a 0 in the next position (64), as 128 + 64 > 179. We place a 1 in the next two position's to add 32 and 16, leaving us at 176. 4 would exceed 179, so we place a 0 in that position, and then we finally tack on two 1's for the final result of 176 + 2 + 1 = 179, represented in binary as 10110011.
179 in hexadecimal, which is a base-16 numeral system often used to represent the memory adresses in a computer's random-access memory (RAM), is quite different. Since hexadecimal requires 16 numerals to stipulate a given number, and there are only 10 single-digit numbers available (0-9), hexadecimal uses the first 6 letters of the alphabet for the remaining numerals, as follows:
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
Thus, 179 in hexadecimal would be:
179 = B3 = (11*16) + (3*1) = 179.
In hexadecimal each additional digit added to the left represents an increase by a factor of 16, such that if there were three digits in a hexadecimal number, the third, left-most, digit would represent 256 (16*16). Oftentimes, you will see a prefix of 0x before the hex values simply to denote that it is a hexadecimal number (for example, 0xB3), although this prefix varies between computer systems and programming languages.
Finally, there is the octal numeral system (base-8), often seen in Unicode character encodings and the chmod (change mode) Unix system command for altering file permissions. Octal uses the numerals 0-7 to represent numbers. 179 in octal, then, goes as follows:
179 = 263 = (2*64) + (6*8) + (3*1) = 128 + 48 + 3 = 179
Phew, why are there so many numeral systems?!