By Miranda Copenhaver, Nancy Hindman*, Efiotu Jagun, and Gloria Su.
*Nancy helped the group work through the problem set but was unfortunately unable to attend camp during the blog writing.
Number systems are how we represent numbers like 1, 32, and 75. We use the base ten (decimal) system for our numbers most of the time. It’s called base ten because it uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. But what if I told you that 1001101 and 4D both mean seventy-seven? Crazy, right? There are countless number systems, but today we will be focusing on two: hexadecimal (base sixteen) and binary (base two)!
As we’ve said before, the binary system is base two; it only uses 0 and 1. Since only 1 or 0 can be used, the placement of each digit is important. Computers use binary to store and transfer information. It is used in communication (Morse code, braille) and everything electronic like computers, lights, calculators, MP3s, MIDI, JPEG, etc.
Hexadecimals are base sixteen. Since there are only ten single-digit numbers (0-9), we have to use letters to signify double-digit numbers: A, B, C, D, E, and F. Each hexadecimal is four bits, so they are more efficient at storing information and easier/faster to work with than binary. There are also fewer mistakes in programming. It is used in IP addresses, HTML, MAC (Media Access Control) addresses, CSS color codes.
Bits and Bytes
Bits and bytes are units of data transmitted through online networks. The “Mega” in megabits (Mb) and megabytes (MB) means bits and bytes but in thousands which is why it’s the preferred way to express data transfer rates. For example: 6Mb/sec.
Bits are used by computers to represent information digitally. They are represented as binary values, so they can either have a value of 1 or 0. Encoding is the process of converting the computer voltages into bits for efficient transmission.
Bytes are similar to bits. A byte is simply made of eight bits. Computers organize data into bytes to increase the efficiency of data processing network disks, equipment and memory.
If you know these rules:
8 bits = 1 byte
1,024 bytes = 1 kilobyte
1,024 kilobytes = 1 megabyte
1,024 megabytes = 1 gigabyte
1,024 gigabytes = 1 terabyte
It is really easy to convert bit values into byte values and vice versa.
For example, to convert 4 kilobytes into bits, you’d use the second conversion to get 4,096 bytes (1,024 X 4) and then the first to get 32,768 bits (4,096 X 8).
Conversion Between Different Bases
We count in the decimal – or base 10 – system. This means that we count using 10 basic numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. In base 10, each place value represents a power of 10.
In the number 5,762 , we can better visualize this concept. So,
5762 = (5*10^3)+(7*10^2)+(6*10^1)+(2*10^0)
This same concept is true in other bases. For example, base five only uses the digits 0, 1, 2, 3, and 4, and each place value represents a power of five starting with 50 (or 1) on the very right and increasing in powers as you go to the left.
The two most important bases in coding are binary (base two) and hexadecimal (base sixteen).
Binary is quite simple to think about because it only has two numbers that you could possibly use: 1 and 0.
Let’s look at an example of converting from binary to base 10:
If we had the number 1101010, we would start by labeling each place value with what power of two it represents. Next, we would multiply each digit by its power of two and simplify:
1101010 = 1*2^6 + 1*2^5 + 0*2^4 + 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0
= 2^6 + 2^5 + 2^3 + 2^1
= 64 + 32 + 8+ 2
We see that the binary number 1101010 is the decimal number 106.
Hexadecimal is slightly more complicated. Since it’s base 16, there have to be 16 digits to represent numbers, except we only have 10: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. To represent the next 6 numbers (10-15), we have to use letters. So, in base 16, A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15.
Let’s look at an example of converting a hexadecimal number to base 10:
If we have the number C3A5, we would start by labeling each place value with what power of 16 it represents:
Next, we would multiply each digit by its power of two and simplify, keeping in mind that letters have numerical equivalents.
C3A5 = 12*16^3 + 3*16^2 + 10*16^1 + 5*16^0
= 12*4096 + 3*256 + 10*16 + 5*1
= 49152 + 768 + 160 + 5
We see that the hexadecimal number C3A5 equals the decimal number 50085.
So why are binary and hexadecimal important? Because they help us program computers and store data in computers. The bits and bytes that we talked about earlier are written in binary, and are vital to keeping information in our computers. Every computer has an IP address written in hexadecimal that lets it be easily identified. Furthermore, binary and hexadecimal let us write in coding languages, which help us program computers to do functions as basic as adding two numbers to things as complicated as building entire social media websites.
An example of code that converts numbers from any base into decimal is shown below.