Number Systems: Binary and Hexadecimal Tom Brennan
Base 10
The base 10 number system is the common number system.
612 = 2 · 100 + 1 · 101 + 6 · 102
2 + 10 + 600 = 612
4365 = 5 · 100 + 6 · 101 + 3 · 102 + 4 · 103
5 + 60 + 300 + 4000 = 4365
Binary
101010101 = 1 · 20 + 0 · 21 + 1 · 22 + 0 · 23 + 1 · 24 + 0 · 25 + 1 · 26 + 0 · 27 + 1 · 28 = 341
1 + 0 + 4 + 0 + 16 + 0 + 64 + 0 + 256 = 341
The binary number system employs only two digits–0 and 1–and each digit is called a bit. A number can be represented in binary by a string of bits–each bit either a 0 or a 1 multiplied by a power of two. For example, to represent the decimal number 15 in binary you would write 1111. That string stands for
1 · 20 + 1 · 21 + 1 · 22 + 1 · 23 = 15 (1 + 2 + 4 + 8 = 15)
History
Gottfried Leibniz developed the modern binary system in 1679 and drew his inspiration from the ancient Chinese. Leibniz found a technique in the I Ching–an ancient chinese text–that was similar to the modern binary system. The system formed numbers by arranging a set of hexagrams in a specific order in a manner similar to modern binary. The systems base two nature was derived from the concepts of yin and yang. This system is at least as old as the Zhou Dynasty–it predates 700 BC. While Leibniz based his system of the work of the Chinese, other cultures had their own forms of base two number systems.
The Ancient Egyptians employed a type of base two number system called Horus-Eye fractions for their measurements of grain or liquids. The system used sums of binary fractions–which were , , , 1/16, 1/32, and 1/64–to represent fractions of hekats, which were the primary unit of measure in Ancient Egypt and equal to about 4.8 liters. Horus-Eye fractions were in use prior to 1200 BC. Ancient Egyptian multiplication also operated with a system similar to that of the binary number system. The Ancient Indians also had a form of binary. Its purpose differed from those of China and Egypt in that it was meant to describe poetic metre. It was created by the scholar Pingala in about 150 BC, and its duality was derived from describing syllables as either short or long.
Applications
Binary is used extensively in computer science and telecommunications. It usefulness is derived from the fact that only two digits are required, so a computer can represent the digit 1 with an electrical signal or 0 with no electric signal. The American Standard Code for Information Exchange is used to represent text and symbols within computer memory. It employs seven digit binary strings–which can represent values between 0 and 127(as 26 + 25 + 24 + 23 + 22 + 21 + 20 = 127). Each symbol is represented by a value between 0 and 127. The lower case letter ”b”, for example,is assigned the number 98 represented by the bit string 1100010 in the ASCII system. Eventually, extended ASCII sets would be released by different computer companies–such as IBM–where data would be processed in 8 bit strings called bytes. The addition of an 8th bit increases the number of possible values excluding 0 from 127 to 255 (because 27 = 128 and 128+127=255).
The Braille alphabet also relies on a binary system–because in each position a dot can either be raised from the page or not raised from the page. Information in Braille is organized into cells–rectangular blocks with tiny bumps called raised dots. Each cell has two columns and 3 rows, and there are 63 different cells in braille if you do not count the space.
Morse code can be transmitted as a binary code. Telegraph operators send electrical pulses along a telegraph wire, and the presence of a pulse–the on state–represents a one while the absence of a pulse–the off state– represents a 0. A bit string of 1 represents a short mark, 111 represents a longer mark, 0 represents the gap between the dots and dashes within a character, 000 represents the gap between letters, and 0000000 represents the gap between words.
The gray code–also known as the reflected binary code–is a form of binary number system that is employed in color television and bar code scanners. It was first created to address a specific problem with the binary number system that is encountered when using physical switches. In many instances consecutive numbers do not share common digits in their binary representations. The quintessential example of this occurs with the numbers three and four. Three is represented by 011 and four is represented by 100. To switch from three to four all three switches must change state, and it is very unlikely that they all three changes will occur at the exact same time. This can lead to some ambiguity when transmitting codes, as it may be difficult to tell when switches are displaying a position or an intermediate between two positions. Gray code differs from the traditional binary systems by only changing one switch at a time. Gray codes have been extremely useful in the field of telegraphy–Emile Baudot received the French Legion of Honor medal for his work in telegraphy with Gray codes in 1878. However, they were made famous and named for Frank Gray, who figured out how to use a machine called the ”PCM tube” to convert analog signals to gray codes in 1953–this method was key in the development of color television.
Binary Addition
Binary addition functions in a manner very similar to decimal addition. However, instead of carrying a one every time two digits add to 10 or a value greater than 10, a one is carried every time two digits add up to 2 or greater(this could only occur if both binary digits were one and a one is carried from the addition of the previous digits.
1010+1111
First Column: 0+1 =1
2 Second Column: 1+1=0 (one will be carried)
Third Column: 0+1+1 (the carried one)=0 (another one will be carried)
Fourth Column: 1+1+1(carried one)=1 (one will be carried)
Fifth Column: 0+0+1(the carried one)=1
So the sum 1010+1111 is 11001.
An easy way to verify this result is to convert the three binary numbers to decimal numbers.
The binary number 1010 converts to 10 (1 · 23 + 1 · 21 = 10)
The binary number 1111 converts to 15 (1 · 23 + 1 · 22 + 1 · 21 + 1 · 20 = 15)
The binary number 11001 converts to 25 (1 · 24 + 1 · 23 + 1 · 20 = 25)
It is easy to verify that 10 + 15 = 25
Binary Subtraction
Binary subtraction is more complicated than binary addition due to the fact that borrowing is often necessary in binary subtraction. Borrowing in binary is similar to borrowing in decimal, but instead of borrowing a 1 · 101 you borrow a 1 · 21. This can get complicated when you have to borrow from a 0 or multiple 0s, like when subtracting 111 from 1000. In situations like these it may be simpler to convert to decimal and then subtract. For that example the difference is one (8-7=1). Pattern recognition can also be useful. 100-11=1, 10000-1111=1, and 100000-11111=1, or in more general terms, the difference between a string with a one followed by n zeroes and a string of n ones is 1.
10101-1111
First Column: 1-1=0
Second Column: 0-1 (in this instance it is necessary to borrow) 10-1=1
After borrowing in the second column the remaining three digits of the larger number are 100. 100-11=1.
So difference between 10101-1111= 110. We can verify this result through decimal subtraction. 21 (24+22+20 = 21) - 15 (23 + 22 + 21 + 20) = 6 (22 + 21 = 6)
Most computers use a method called the complement method to subtract binary numbers.
The method has five steps. The first is to add zeros to the end of the smaller binary string if necessary to ensure that the two strings have the same number of bits. The second step is to switch all digits in the second term–all zeros should be switched to ones and all ones switched to zeros. The third step is to add one to the new second term. The fourth step is to add the the two terms together. And the fifth and final step is to discard the first term–as there will be an extra digit.
10101-1111
10101
01111 –
3 complement of second string: 10000 add one to second string: 10001 addition of the two strings
10101+10001=100110 discard the first digit
00110
Binary Multiplication
Like binary addition, binary multiplication is very similar to decimal multiplication. The main difference between the two is that when adding up the results of the multiplication–in the same manner as decimal multiplication–a one is carried when the digits add to two or greater instead of ten or greater.
1010
1111 x
—1010
–1010
-1010
1010 +
10010110
This in decimal converts to 150 (27 + 24 + 22 + 21 = 150)
We can verify that this multiplication is correct easily. 1010 in decimal is 10 and 1111 in decimal is 15. 15 ∗ 10 = 150
Hexadecimal
The Hexadecimal number system employs sixteen digits: 0-9 represent themselves and then 10-15 are repre- sented by a,b,c,d,e, and f. To differentiate hexadecimal numbers from decimal numbers the can be written with subscripts. For example the hexadecimal number 14, which is equal to 20 in decimal, can be written as 14hex or 1416.
History
4 John W Nystrom first championed the use of a base 16 number system called the Tonal System in 1859. In the early days of computers, not all computer developers used a-f as digits 10-15. The digits 0 to five with a macron were used by some developers, the letters U through Z were used by Bendix G-15 computers, the letters F, G, J, K, Q, and W were used by the Librascope LGP-30, K, S, N, J, F, and L were used by the ILLIAC I computer, and there were many other letters and symbols employed for digits 10-15. Some computer scientists–Bruce Alan Martin of Brookhaven National Laboratory foremost among them–sharply criticized the choice of the letters a-f for the last six hexadecimal digits.
Applications
The conversion between binary and hexadecimal numbers is very simple–which makes hexadecimals a conve- nient way of representing large binary strings. Only two hexadecimal digits are required to represent every byte–which is equal to eight bits. This is an improvement from the decimal system, in which three digits would be required, and a vast improvement over the binary system, which would need eight digits. This property of the hexadecimal system–along with the fact that it is much easier to read a hexadecimal string than a binary string–is the reason that it is frequently used by computer programmers to describe locations in memory. Only two digits of hexadecimal are required to represent all 255 locations in the extended ASCII system–as ff, the largest two digit hexadecimal, is equal to 255 ( 15 ∗ 161 + 15 ∗ 160 = 240 + 15 = 255)
Hexadecimals are also used to describe colors in programs. Two hexadecimal digits are used to represent each of the primary colors. So there are 255 possible numerical representations of each primary color. Colors are represented with six digits–two for each prime–and therefore there are 16581375 possible colors due to the fact that 255 ∗ 255 ∗ 255 = 16581375.
Conversions
Converting from binary to hexadecimal is quite simple. You simply group together four binary digits, add their value, and replace them with their corresponding hexadecimal digit.
1111 = f
1 ∗ 20 + 1 ∗ 21 + 1 ∗ 22 + 1 ∗ 23 = 15 = f
1010111101011100
(0 · 20 + 1 · 21 + 0 · 22 + 1 · 23) = 10 = a
(1 · 20 + 1 · 21 + 1 · 22 + 1 · 23) = 15 = f
(1 · 20 + 0 · 21 + 1 · 22 + 0 · 23) = 5 = 5
(1 · 20 + 0 · 21 + 1 · 22 + 0 · 23) = 12 = c
5 1010111101011100 = af5c
In order to convert hexadecimal numbers to binary numbers, you take each digit of the hexadecimal number and represent it with four equivalent binary digits.
a(hexadecimal representation of 10)=1010
a5dc
a(10) = 1010
5 = 0101
d(13) = 1101
c(12) = 1100
a5dc = 1010010111011100
In order to convert hexadecimal numbers to decimal numbers, you simply calculate the base 10 value of each digit and then add the total. This is slightly more complicated than the the hexadecimal to binary conversion.
af5c = (10 · 163) + (15 · 162) + (5 · 161) + (12 · 100) =
40960 + 3840 + 80 + 12 = 44892
af5c = 44892
In order to convert decimal numbers to hexadecimal, you start by dividing the decimal number by 16 and recording the remainder. You divide the result by 16 and record the remainder and continue to do so until the result is 0. Each remainder is a hexadecimal digit, and the sequence of remainders is the hexadecimal number in reverse order.
1128/16=70.5 (so 70 with a remainder of 8)
70/16=4.375 (so 4 with a remainder of 6)
4/16= 0 (with a remainder of 4)
6 So 1128 in hexadecimal is 468. (816˙ 0 + 6 · 16 + 4 · 162 = 1128)
Hexadecimal Addition
Hexadecimal addition functions similarly to decimal addition–however, instead of carrying a one when the value of the sum of two digits exceeds 10, a one is carried when the value of the sum of two digits exceeds 16.
6b+7c c+b= 7 (carry a one)
6+7+1= e
So 6b+7c=e7. We can verify this result using decimal addition.
(6b = 6 · 16 + 11 = 107), (7c = 7 · 16 + 12 = 124), and (e7 = 14 · 16 + 7 = 231)
107+124=231.
Hexadecimal Subtraction
Hexadecimal subtraction differs from decimal subtraction in that you borrow a 16 instead of a ten when borrowing is necessary.
29- 1e
9-e (need to borrow) 19-e=b
1-1= 0
We can verify that 29-1e=b through decimal subtraction.
29 in hexadecimal is equal to 41 in decimal (16 · 2 + 9 = 41) and 1e in hexadecimal is equal to 30 in decimal (16 · 1 + 14 = 30) and 41-30=11.
Hexadecimal Multiplication
Multiplication of hexadecimals is very similar to the multiplication of base ten numbers. However, differ- ences arise when multiplying each digit due to different carrying rules and the differences in hexadecimal representation of sums. Also, the hexadecimal addition rules must be followed in the addition step.
23
×3b
–181
+690
811
The first line is 181 because the product of b and 3 is 21 (because 11 ∗ 3 = 33 and 2 ∗ 16 + 1 = 33), so in
7 the first line column you put a one and then carry a two into the second column. Then when you multiply b and 2 you get 16, (because 11 ∗ 2 = 22 and 1 ∗ 16 + 6 = 22) so in the second column you put 8 because of the carried two from the prior multiplication and you carry a one to the next column. In the second row the multiplication is much simpler due to the fact that you will not need to carry as each of the products are smaller than 16–in this case smaller than 10 as well–so the second row works exactly as it would with decimal multiplication. 3 ∗ 3 = 9 and 3 ∗ 2 = 6
(23 = 2 ∗ 16 + 3 = 35), and (3b = 3 ∗ 16 + 11 = 59), so we can verify through decimal multiplication that 59 ∗ 35 = 2065, and (8 ∗ 162 + 1 ∗ 16 + 1 = 2065).
Rational numbers
Hexadecimal numbers can represent fractions, although the hexadecimal system is less convenient for repre- senting rational numbers because only fractions with denominators that are a power of two yield terminating decimals. In any number system the value .1 is equal to one divided by the base value in its own number system. Therefore the fraction .1 is equal to 1/10hex or 1/16dec. Here are some sample values of fractions in the decimal system.
1/2 = 0.8 :: 1/3 = 0.5 :: 1/4 = 0.4
1/5 = 0.3 :: 1/6 = 0.2A :: 1/7 = 0.249
1/8 = 0.2 :: 1/9 = 0.1C7 :: 1/A = 0.19
1/B = 0.1745D :: 1/C = 0.15 :: 1/D = 0.13B
1/E = 0.1249 :: 1/F = 0.1 :: 1/10 = 0.1 :: 1/11 = 0.0F
Miscellaneous
Hexadecimals play a key role in the movie The Martian as Mark Watney uses them to communicate with NASA. While Mark is initially stranded on Mars with no way to communicate with Earth, he eventually finds and digs up the the Mars Pathfinder. After fixing the probe, its camera feeds directly to NASA, reestablishing some form of communication. Mark can send messages to NASA by writing them out on paper and placing the paper in front of the camera. However, NASA initially has no way of communicating back, and the only power they have is to rotate the camera 360 degrees. At first Mark tries to set up an alphabet in a circle around the pathfinder camera, so that the camera can point to different letters to send a message from NASA, but the camera’s motion would not easily allow it to point to 26 different letters. However, the camera could more easily point to sixteen different characters, so Mark sets up sixteen different signs around the pathfinder camera marked 0-9 and a-f to represent the hexadecimal digits. From there NASA could send a message by rotating the camera to face different digits. Every two digits would represent one letter or symbol based on ASCII rules–letters and symbols would be assigned values between 0 and 255, as ff, the largest two digit hexadecimal, is equal to 255.
Legend has it that a famous wizard with 8 fingers on each hand invented the hexadecimal counting system. Unfortunately, that legend is false.
Octal System
The Octal System was the predecessor of the Hexadecimal system. Computer Scientists for convenience would group binary bits together in threes–this technique would eventually be replaced by grouping the digits by
8 four as computers started to use more complicated bit systems–giving rise to a base 8 system of numbers. The octal system employs digits 0 through 7 and octal numbers are written with a lowercase ”o” before the number ( ie o306). Interestingly enough, before its application in computer science, octal counting was used by Native American tribes in California and Mexico (tribes using the Yuki and Pamean languages) because they counted the spaces between their fingers instead of the fingers themselves.
To convert binary numbers to octal numbers you simply group–or chunk–three bits together and represent the value of those three binary digits with their value in an octal digit (which in this case would be the same as their value in the decimal system). For the binary string 101111100 the octal number equivalent is o574
101 = 1 · 22 + 1 · 20 = 5
111 = 1 · 22 + 1 · 21 + 1 · 20 = 7
100 = 1 · 22 = 4
Converting octal numbers to decimal numbers is very similar to converting hexadecimal numbers to decimal numbers: you find the base 10 value of each digit and then add the values together. We will convert the octal number that we found in the last example into a decimal number.
5 · 82 + 7 · 81 + 4 · 80 = 380
When converting the original binary string to a decimal number (28 + 26 + 25 + 24 + 23 + 22) we also reach the value of 380. This verifies that our conversions are consistent with each other.
To convert octal numbers to binary numbers you simply represent each octal digit with an equivalent three digit binary string. For our example of o574 the equivalent string would be 101111100
o5 = 22 + 20 = 101
o7 = 22 + 21 + 20 = 111
o4 = 22 = 100
The most efficient way to convert between hexadecimal numbers and octal numbers is to convert the number to its binary string equivalent and then convert to the other number system.
For example ffhex=o377.
ff=1111 1111
011 111 111= 377.
9 We can verify that the conversion was done correctly by showing that both ffhex and o377 equal 255 in the decimal system. (15 ∗ 16 + 15 = 255) and (3 ∗ 82 + 7 ∗ 8 + 7 = 255)
Converting from decimal to octal relies on a mechanism that is very similar to the conversion from decimal to hexadecimal. The number is first divided by 8, and the remainder is recorded, then the quotient(or the greatest integer less than the quotient) is divided by 8, and the remainder is recorded, and this process is repeated until the quotient is 0. The octal number is the remainders written in reverse order. For example, the decimal number 110 is equal to o156.
110/8 = 13.75 (The remainder is 6)
13/8 = 1.625 (The remainder is 5)
1/8 = .125 (The remainder is 1) So the final octal value is 156. We can verify that this answer is correct by converting o156 back to decimal. 1 ∗ 82 + 5 ∗ 8 + 6 = 110.
Quinary
The Quinary system is a base five number system. It most likely traces its roots back to the fact that a hand has five fingers. Base five number systems are employed by some languages, including Saraveca and Gumatj. A decimal system that has two and five as sub bases is called biquinary–and it is used in the ancient abacus.
Some decimal numbers in quinary form
1=1
2=2
3=3
4=4
5=10
6=11
7=12
8=123
9=14
10=20
15=30
20=40
25=100
Sexagesimal
The Sexagesimal system is a base 60 number system that is about four thousand years old. It traces back to the Sumerians and was prominant in Babylonian mathematics. Sixty is the least common multiple of 1, 2, 3, 4, 5, and 6, and has 12 factors. Because of this there are many simplified fractions in the sexagesimal
10 system. The sexagesimal system is still used today in the measurement of time. Every hour is divided into 60 minutes, with each minute being divided into 60 seconds. Therefore, a time such as 2:15:09 can be viewed as a sexagesimal number 2 ∗ 602 + 15 ∗ 601 + 09 ∗ 600. However, unlike the other number systems explored, the sexagecimal digits–2, 15, and 09–were written using the decimal system. Angle measure also relies on a sexagecimal system. There are 360 degrees in a circle, so there are six sixty degree segmentsm, there are 60 minutes of arc in each degree, and there are 60 arcseconds in each minute. Geographical coordinates also rely on a sexagesimal system.
Duodecimal
The Duodecimal system is a base 12 number system invented by Sir Isaac Pitman. It typically represents the digit 10 with a rotated 2 and the digit eleven with a rotated three, but sometimes a and b are used for 11 and 12, respectively. The advantage of the duodecimal system is derived from the fact that twelve is the smallest number with four non trivial factors–2, 3, 4, and 6. This makes the duodecimal system the most convenient number system for the representation of fractions; in fact, the fractions 1/2, 1/3, 2/3, 11/4, and 3/4 all have terminating representations (.6, .4, .8, .3, and .9). Because of this, and the fact that it avoids the supremely unwieldy multiplication tables inherent in the trigesimal and sexagesimal number systems, some scholars and mathematicians argue that the duodecimal system is the ideal number system.
11 References
1. http://www.permadi.com/tutorial/numDecToHex/
2. http://web.math.princeton.edu/math_alive/1/Lab1/BinAdd.html
3. http://http://www.linfo.org/hexadecimal.html
4. http://thestarman.pcministry.com/asm/hexawhat.html
5. http://www.electronics-tutorials.ws/binary/bin_4.html
6. https://www.mathsisfun.com/binary-number-system.html
7. http://cs.stackexchange.com/questions/19963/why-hex-octal-or-hexadecimal-computers-use-binary-and-humans-decimals
12