Programming Fundamentals - part 1 of 2
We begin our investigation of programming fundamentals by reviewing the number systems most commonly used in computer science and programming languages. We consider the binary, decimal, hexadecimal, and octal number systems. It is important to understand that the methods used below to describe one number systems can be used to explain other number systems. So even though we do not consider an unusual number system such as base 12, it should be easy enough to describe that system using the same techniques shown below.
It seems appropriate to initiate our discussion of programming fundamentals by reviewing number systems. We are most familiar with the decimal system which is the prevalent system in use worldwide. The decimal system uses radix 10 (base 10). Even though we think in decimal terms, we might not understand how the system is derived. We can 'derive' the decimal number using the same operations we used in the binary, hexadecimal, and octal tables shown below. Naturally, understanding the value of a decimal number is second nature so we really don't have to perform the operations of multiplication and addition by each exponential place. However, it is important to understand that decimal numbers are constructed using the same methods of the other number systems.
|0 + 3000000 + 500000 + 90000 + 2000 + 600 + 80 + 7 = 3,592,687|
The binary system is the most elemental number system used in computing. In binary, one digit is called a bit, four bits a nibble (or nybble), and eight bits a byte. The definition of the term 'word' when referring to number of bits varies. It usually means the number of bits in the CPU registers so it is commonly 16, 32, or 64-bits. In the formative days of computing, binary was originally used to identify the presence (1) or absence (0) of an electrical charge. There are two digits used in the binary system and those two numbers can be combined to represent any positive numerical value. Binary, meaning 'two', consists of the numbers 1 and 0. In the first row below, the number 2 is called the radix (a.k.a. base 2). It is raised to the powers 0 - 7. The first row of all number systems consist of the radix raised to values starting at 0. Recall from basic math class that any number raised to 0 is equal to 1.
The second row contains values of the resulting exponential operation. For instance, 27 = 128, 24 = 16, etc. How do we convert a binary number to its decimal equivalent? The binary number 10101101 is provided as an example. To determine the decimal equivalent of that binary number, perform the appropriate multiplications and additions shown in the table. Note the value of 1 set on the third row in the 25 position. This means that 32 should be multiplied by that 1 to produce 32 which is then added with the results from the other positions. The appropriate operations show that 10101101 in binary = 173 in decimal.
|128 + 0 + 32 + 0 + 8 + 4 + 0 + 1 = 173|
Since binary numbers are difficult to read, another popular number system was developed. In computing, the hexadecimal (hex) system is ubiquitous. The hex system has the number 16 as its radix (base 16). Only one hex digit is required to represent every four binary digits. Hex numbers are seen throughout computing in values such as colors, address locations, and assembly language instructions.
Hex numbers are identified by a leading '0x' as in '0x7E23FA'. Sometimes they are shown with a lowercase 'h' such as '7Dh'. Observe the letters in the third row of the hexadecimal table. Why are the letters E, F, and A used? In the binary system we needed two digits (0 and 1) and in the decimal system we needed ten digits (0-9) to represent the numbers. In the hex system we need 16 digits. However, we only have ten digits, 0-9 symbols with which to work. Therefore, we need six more symbols. Since we are out of numerical symbols after 0 - 9, we use first six letters from the alphabet A -F. So, A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Using the symbols 0-9 and A-F, we now have 16 symbols with which to work.
Now consider the hex number '0x7E23FA' as shown in the table. How do we obtain the decimal equivalent of that number? We use the same technique as that used for binary and decimal numbers. For instance, 164 = 65536. Recall that E = 14. So, if we have E in the 164 position, we multiply 65536 x 14 = 917540 as shown. Perform the same multiplication operation for the other positions in the table, add the products to obtain the decimal equivalent of '0x7E23FA' which we see to be 8,266,746.
|0 + 0 + 7340032 + 917504 + 8192 + 768 + 240 + 10 = 8,266,746|
There are only eight numbers required/used in the octal system. Seeing numbers expressed in octal is less common than it was in the early days of computing. Early computers had 12, 24, or 32-bit address spaces and each of those is divisible by three. One octal number can be used to represent three binary digits so it was a convenient method for referencing addresses.
However, more recently, computers are using 32 and 64-bit addressing so the hex system is more compatible since both 32 and 64 are divisible by 16 and can be more succinctly described than with octal digits. For instance, the 16-bit binary number 11001110 11110001 is equal to CEF1 in hex and 147361 in octal. Two more digits are required to represent the number in octal compared to hex. Octal can still be found in computing applications such as setting Unix file permissions.
The calculator that ships with Windows 10 includes a 'programmer' version which can be accessed via the View menu. In the image below, I originally entered the binary number (highlighted) and then selected 'Hex' to see the hex equivalent. Note that the binary number is still displayed. And, the binary values can be edited from positions 0 - 63 in the orange oval section. The radix can be selected in the blue rectangle section. Note the letters A - F that are enabled when Hex is selected.
The same multiplication and addition operations that were used in the binary and hex systems above are applicable to octal. So, in the example below, the octal number 0o20053107 is equal to the decimal number 4,216,391.
|4194304 + 0 + 0 + 20480 + 1536 + 64 + 0 + 7 = 4,216,391|
Multiples of bits by orders of magnitude (powers of 10) are assigned names per the chart below.
Multiples of bytes by orders of magnitude (powers of 10) are assigned names per the chart below.