Recall that memory cells are commonly grouped together to form larger units of memory. The smallest unit of grouped memory cells is called a byte. A byte is a unit of data containing 8 bits. The following table shows a list of common data unit sizes:
Unit | Symbol | Bits/Bytes |
---|---|---|
Byte | B | 8 bits |
Kilobyte | KB | 1024 bytes |
Megabyte | MB | 1,048,576 bytes |
Gigabyte | GB | 1,073,741,824 bytes |
Terabyte | TB | 1,099,511,627,776 bytes |
Petabyte | PB | 1,125,899,906,842,624 bytes |
Exabyte | EB | 1,152,921,504,606,846,976 |
Zettabyte | ZB | 1,180,591,620,717,411,303,424 bytes |
At first glance these numbers may seem arbitrary and hard to remember, but they are actually quite exact and easy to calculate. Each subsequent memory unit is just 1024 units of the previous memory unit.
1 byte * 1024 = 1024 bytes = 1 KB
1024 bytes * 1024 = 1,048,576 bytes = 1 MB
1,048,576 bytes * 1024 = 1,073,741,824 bytes = 1 GB
1,073,741,824 bytes * 1024 = 1,099,511,627,776 bytes = 1 TB
...
To store bits of data in standardized manageable chunks computers use these memory units. These bits of data are referred to as binary, which is not very easy to ready. Thus for humans to be able to read the data it must be converted to other number systems.
Numbers in the human world are generally written using decimal--the number system that comprises of the digits 0
thru 9
. This numbering system is generally referred to as base10. Computers do not do not speak in base10, human speak, though. Instead computers store and display instructions and data in binary base2, and sometimes display data in base16 to make it easier for humans to read (since the numbers will be smaller). Since instructions and data are stored in memory cells and run on the CPU as 0's and 1's every instruction and piece of data must be converted at some point to 0's and 1's so that they can be stored in memory and run on the CPU.
Let n be the decimal number.
Let m be the number, initially empty, that we are converting to. We'll be composing it right to left.
Let b be the base of the number we are converting to.
Repeat until n becomes 0
Divide n by b, letting the result be d and the remainder be r.
Write the remainder, r, as the leftmost digit of b.
Let d be the new value of n.
Let n be the number of digits in the number.
Let b be the base of the number.
Let s be a running total, initially 0.
For each digit in the number, working left to right do:
Subtract 1 from n.
Multiply the digit times bn and add it to s.
When your done with all the digits in the number, its decimal value will be s.
Binary--the number system that comprises of the digits 0
and 1
--is the name for the number system base2. Since base2 only uses the digits 0
and 1
it is perfect for representing bits.
Following the algorithm to convert from base10 to basen numbers in decimal (base10) can be converted to binary (base2).
Example 1
Convert 5710 to binary (base2). Show the result using 1 byte.
First set n, m, and b to:
n
= 57
m
= ""
b
= 2
n | b | result | remainder | m | |||
---|---|---|---|---|---|---|---|
57 | ÷ | 2 | = | 28 | + | 1 | 1 |
28 | ÷ | 2 | = | 14 | + | 0 | 01 |
14 | ÷ | 2 | = | 7 | + | 0 | 001 |
7 | ÷ | 2 | = | 3 | + | 1 | 1001 |
3 | ÷ | 2 | = | 1 | + | 1 | 11001 |
1 | ÷ | 2 | = | 0 | + | 1 | 111001 |
Thus giving the result 5710 => 1110012
Since the result wants the result in 1 byte this means the result must be shown using 8-bits. Since 111001
only has 6
digits it must be padded (on the left) with 0
's, which does not affect the number.
Thus the final result is: 001110012
Example 2
Convert 2857410 to binary (base2). Show the result using 2 bytes.
First set n, m, and b to:
n
= 28574
m
= ""
b
= 2
n | b | result | remainder | m | |||
---|---|---|---|---|---|---|---|
28574 | ÷ | 2 | = | 14287 | + | 0 | 0 |
14287 | ÷ | 2 | = | 7143 | + | 1 | 10 |
7143 | ÷ | 2 | = | 3571 | + | 1 | 110 |
3571 | ÷ | 2 | = | 1785 | + | 1 | 1110 |
1785 | ÷ | 2 | = | 892 | + | 1 | 11110 |
892 | ÷ | 2 | = | 446 | + | 0 | 011110 |
446 | ÷ | 2 | = | 223 | + | 0 | 0011110 |
223 | ÷ | 2 | = | 111 | + | 1 | 10011110 |
111 | ÷ | 2 | = | 55 | + | 1 | 110011110 |
55 | ÷ | 2 | = | 27 | + | 1 | 1110011110 |
27 | ÷ | 2 | = | 13 | + | 1 | 11110011110 |
13 | ÷ | 2 | = | 6 | + | 1 | 111110011110 |
6 | ÷ | 2 | = | 3 | + | 0 | 0111110011110 |
3 | ÷ | 2 | = | 1 | + | 1 | 10111110011110 |
1 | ÷ | 2 | = | 0 | + | 1 | 110111110011110 |
Thus giving the result 2857410 => 1101111100111102
Since the result wants the result in 2 bytes this means the result must be shown using 16-bits. Since 110111110011110
only has 15
digits it must be padded (on the left) with 0
's, which does not affect the number.
Thus the final result is: 01101111100111102
Following the algorithm to convert from basen to base10 numbers in binary (base2) can be converted to decimal (base10).
Example 1
Convert 1012 to decimal (base10).
First set n, b, and s to:
n
= 3
b
= 2
s
= 0
n | digit | ||
---|---|---|---|
2 | 1 | ||
1 | 0 | ||
0 | 1 |
Thus giving the result 1012 => 510
Example 2
Convert 11010012 to decimal (base10).
First set n, b, and s to:
n
= 7
b
= 2
s
= 0
n | digit | ||
---|---|---|---|
6 | 1 | ||
5 | 1 | ||
4 | 0 | ||
3 | 1 | ||
2 | 0 | ||
1 | 0 | ||
0 | 1 |
Thus giving the result 11010012 => 10510
Hexadecimal--the number system that comprises of the digits 0
thru 9
, as well as the characters A
thru F
--is commonly used to display large binary values in a shorter form. A common place hex numbers are used is when displaying memory addresses as there are billions of addresses in a computer (which results in large strings of bits for addresses). Hexadecimal is also referred to as base16 meaning that there are 16 digits that can be used. The first 10 digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, but what is used past 9? Characters are pulled from the alphabet to make up the remaining characters leaving us with the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
Following the algorithm to convert from base10 to basen numbers in decimal (base10) can be converted to hexadecimal (base16). One step must be added to the algorithm though. Since with hexadecimal the remainder can be greater than 9 (i.e. 10, 11, 12, 13, 14, and 15) remainders greater than 9 must be converted to their character digits A, B, C, D, E, and F. The following chart shows which remainders correspond to which digit:
Remainder | Digit |
---|---|
10 | A |
11 | B |
12 | C |
13 | D |
14 | E |
15 | F |
Example 1
Convert 2910 to hexadecimal (base16).
First set n, b, and s to:
n
= 29
m
= ""
b
= 16
n | b | result | remainder | m | |||
---|---|---|---|---|---|---|---|
29 | ÷ | 16 | = | 1 | + | 13 | D |
1 | ÷ | 16 | = | 0 | + | 1 | 1D |
Thus giving the result 2510 => 1D16
Example 2
Convert 42710 to hexadecimal (base16).
First set n, b, and s to:
n
= 427
m
= ""
b
= 16
n | b | result | remainder | m | |||
---|---|---|---|---|---|---|---|
427 | ÷ | 16 | = | 26 | + | 11 | B |
26 | ÷ | 16 | = | 1 | + | 10 | AB |
1 | ÷ | 16 | = | 0 | + | 1 | 1AB |
Thus giving the result 42710 => 1AB16
Following the algorithm to convert from basen to base10 numbers in hexadecimal (base16) can be converted to decimal (base10). Again, one step must be added to the algorithm. Since with hexadecimal there are digits that do not directly map to numbers (i.e. A, B, C, D, E, and F) these digits must be converted back to numbers (i.e. 10, 11, 12, 13, 14, and 15). The following chart shows the direct mappings of the non-numerical digits to be used for calculations:
Digit | Value |
---|---|
A | 10 |
B | 11 |
C | 12 |
D | 13 |
E | 14 |
F | 15 |
Example 1
Convert B716 to decimal (base10).
First set n, b, and s to:
n
= 2
b
= 16
s
= 0
n | digit | ||
---|---|---|---|
1 | B | ||
0 | 7 |
Thus giving the result B716 => 18310
Example 2
Convert 1BF7216 to decimal (base10).
First set n, b, and s to:
n
= 5
b
= 16
s
= 0
n | digit | ||
---|---|---|---|
4 | 1 | ||
3 | B | ||
2 | F | ||
1 | 7 | ||
0 | 2 |
Thus giving the result 1BF7216 => 11454610
The keys on the keyboard also need to be able to be represented in a computer. To do this 1 byte of memory is used to represent each key on the keyboard, allowing for 256 keys to be able to be represented. A special table called the American Standard Code for Information Interchange (ASCII) Table has been developed to map the characters on the keyboard directly to decimal numbers which can then be converted to hex or binary. This table can be found:
https://blog.alexstaubin.com/post/ascii-table
ASCII characters are much simpler to convert between character and decimal value as they can simply be looked up on the ASCII Table
Example 1
Convert the ASCII character 'A'
to decimal.
'A'
has the ASCII value 65
.
Thus the answer is 65
.
Example 2
Convert the decimal value 97
to ASCII.
97
is the ASCII character 'a'
.
Thus the answer is 'a'
.
Example 3
Convert the decimal value 1000
to ASCII.
1000
is not a value on the ASCII table, the ASCII table only goes to 255
.
Thus the answer is that 1000
is not an ASCII value.
Decimal - The number system that comprises of the digits 0
thru 9
.
Binary - The number system that comprises of the digits 0
and 1
Hexadecimal - The number system that comprises of the digits 0
thru 9
, as well as the characters A
thru F
What is decimal?
What digits are used in the decimal number system?
What is base10?
What is binary?
What digits are used in the binary number system?
What is base2?
What is hexadecimal?
What digits are used in the hexadecimal number system?
What is base16?
What is the algorithm to convert a decimal number to binary?
What is the algorithm to convert a decimal number to hexadecimal?
When converting to from decimal to another number system, what operation is used to obtain the digits?
What is the algorithm to convert binary numbers to decimal?
What is the algorithm to convert hexadecimal numbers to decimal?
When converting to from another number system to decimal, what operation is used to obtain the digits?
Thinking further: Using the information from this article, what algorithm would be used to convert a binary number to hexadecimal.
Thinking further: Using the information from this article, what algorithm would be used to convert a hexadecimal number to binary.
Why is binary important in computing?
Why do computers use binary instead of decimal?
How does increasing the number of bits affect the maximum value that can be represented?
How many unique values can be represented with 8 bits?
Convert the decimal number 0 to binary.
Convert the decimal number 1 to binary.
Convert the decimal number 13 to binary.
Convert the decimal number 100 to binary.
Convert the decimal number 255 to binary.
Convert the decimal number 0 to hexadecimal.
Convert the decimal number 1 to hexadecimal.
Convert the decimal number 13 to hexadecimal.
Convert the decimal number 111 to hexadecimal.
Convert the decimal number 255 to hexadecimal.
Convert the binary number 0 to decimal.
Convert the binary number 1 to decimal.
Convert the binary number 1101 to decimal.
Convert the binary number 101010 to decimal.
Convert the binary number 11111111 to decimal.
Convert the binary number 1101001 to decimal.
Convert the hexadecimal number 0 to decimal.
Convert the hexadecimal number 1 to decimal.
Convert the hexadecimal number 13 to decimal.
Convert the hexadecimal number AB to decimal.
Convert the hexadecimal number 1A2B3 to decimal.
Convert the hexadecimal number 1101001 to decimal.
Convert the binary number 10 to hexadecimal.
Convert the binary number 1111 to hexadecimal.
Convert the binary number 11001100 to hexadecimal.
Convert the binary number 101010 to hexadecimal.
Convert the hexadecimal number A to binary.
Convert the hexadecimal number ABCD to binary.
Convert the hexadecimal number 12A7F to binary.
Convert the hexadecimal number 101010 to binary.