Data Representation

Tue Jan 28 2025
Updated: Mon Jul 14 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Memory Units

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:

UnitSymbolBits/Bytes
ByteB8 bits
KilobyteKB1024 bytes
MegabyteMB1,048,576 bytes
GigabyteGB1,073,741,824 bytes
TerabyteTB1,099,511,627,776 bytes
PetabytePB1,125,899,906,842,624 bytes
ExabyteEB1,152,921,504,606,846,976
ZettabyteZB1,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.



Conversion Algorithms

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.


Algorithm to Convert from base10 (Decimal) to basen

  1. Let n be the decimal number.

  2. Let m be the number, initially empty, that we are converting to. We'll be composing it right to left.

  3. Let b be the base of the number we are converting to.

  4. 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.


Algorithm to Convert from basen to base10 (Decimal)

  1. Let n be the number of digits in the number.

  2. Let b be the base of the number.

  3. Let s be a running total, initially 0.

  4. 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.

  5. When your done with all the digits in the number, its decimal value will be s.



Binary

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.


Converting Unsigned Integers to Binary

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

nbresultremainderm
57÷2=28+11
28÷2=14+001
14÷2=7+0001
7÷2=3+11001
3÷2=1+111001
1÷2=0+1111001

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

nbresultremainderm
28574÷2=14287+00
14287÷2=7143+110
7143÷2=3571+1110
3571÷2=1785+11110
1785÷2=892+111110
892÷2=446+0011110
446÷2=223+00011110
223÷2=111+110011110
111÷2=55+1110011110
55÷2=27+11110011110
27÷2=13+111110011110
13÷2=6+1111110011110
6÷2=3+00111110011110
3÷2=1+110111110011110
1÷2=0+1110111110011110

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


Converting Binary to Unsigned Integers

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

ndigitv=digitbnv = digit * b^ns=s+vs = s + v
21122=41 * 2^2 = 40+4=40 + 4 = 4
10021=00 * 2^1 = 04+0=44 + 0 = 4
01120=11 * 2^0 = 14+1=54 + 1 = 5

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

ndigitv=digitbnv = digit * b^ns=s+vs = s + v
61126=641 * 2^6 = 640+64=640 + 64 = 64
51125=321 * 2^5 = 3264+32=9664 + 32 = 96
40024=00 * 2^4 = 096+0=9696 + 0 = 96
31123=81 * 2^3 = 896+8=10496 + 8 = 104
20022=00 * 2^2 = 0104+0=104104 + 0 = 104
10021=00 * 2^1 = 0104+0=104104 + 0 = 104
01120=11 * 2^0 = 1104+1=105104 + 1 = 105

Thus giving the result 11010012 => 10510 



Hexadecimal

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.


Converting Unsigned Integers to Hexadecimal

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:

RemainderDigit
10A
11B
12C
13D
14E
15F

Example 1

Convert 2910 to hexadecimal (base16).

First set n, b, and s to:

n = 29

m = ""

b = 16

nbresultremainderm
29÷16=1+13D
1÷16=0+11D

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

nbresultremainderm
427÷16=26+11B
26÷16=1+10AB
1÷16=0+11AB

Thus giving the result 42710 => 1AB16 


Converting Hexadecimal to Unsigned Integers

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:

DigitValue
A10
B11
C12
D13
E14
F15

Example 1

Convert B716 to decimal (base10).

First set n, b, and s to:

n = 2

b = 16

s = 0

ndigitv=digitbnv = digit * b^ns=s+vs = s + v
1B11161=17611 * 16^1 = 1760+176=1760 + 176 = 176
077160=77 * 16^0 = 7176+7=183176 + 7 = 183

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

ndigitv=digitbnv = digit * b^ns=s+vs = s + v
411164=655361 * 16^4 = 655360+65536 =655360 + 65536 = 65536
3B11163=4505611 * 16^3 = 4505665536+45056=11059265536 + 45056 = 110592
2F15162=384015 * 16^2 = 3840110592+3840=114432110592 + 3840 = 114432
177161=1127 * 16^1 = 112114432+112=114544114432 + 112 = 114544
022160=22 * 16^0 = 2114544+2=114546114544 + 2 = 114546

Thus giving the result 1BF7216 => 11454610 



ASCII

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


Converting ASCII to and from Decimal

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.



Terms

  1. Decimal - The number system that comprises of the digits 0 thru 9.

  2. Binary - The number system that comprises of the digits 0 and 1

  3. Hexadecimal - The number system that comprises of the digits 0 thru 9, as well as the characters A thru F



Questions

Number Systems

  1. What is decimal?

  2. What digits are used in the decimal number system?

  3. What is base10?

  4. What is binary?

  5. What digits are used in the binary number system?

  6. What is base2?

  7. What is hexadecimal?

  8. What digits are used in the hexadecimal number system?

  9. What is base16?

Conversion Basics

  1. What is the algorithm to convert a decimal number to binary?

  2. What is the algorithm to convert a decimal number to hexadecimal?

  3. When converting to from decimal to another number system, what operation is used to obtain the digits?

  4. What is the algorithm to convert binary numbers to decimal?

  5. What is the algorithm to convert hexadecimal numbers to decimal?

  6. When converting to from another number system to decimal, what operation is used to obtain the digits?

  7. Thinking further: Using the information from this article, what algorithm would be used to convert a binary number to hexadecimal.

  8. Thinking further: Using the information from this article, what algorithm would be used to convert a hexadecimal number to binary.

Critical Thinking

  1. Why is binary important in computing?

  2. Why do computers use binary instead of decimal?

  3. How does increasing the number of bits affect the maximum value that can be represented?

  4. How many unique values can be represented with 8 bits?

Conversions

  1. Convert the decimal number 0 to binary.

  2. Convert the decimal number 1 to binary.

  3. Convert the decimal number 13 to binary.

  4. Convert the decimal number 100 to binary.

  5. Convert the decimal number 255 to binary.

  6. Convert the decimal number 0 to hexadecimal.

  7. Convert the decimal number 1 to hexadecimal.

  8. Convert the decimal number 13 to hexadecimal.

  9. Convert the decimal number 111 to hexadecimal.

  10. Convert the decimal number 255 to hexadecimal.

  11. Convert the binary number 0 to decimal.

  12. Convert the binary number 1 to decimal.

  13. Convert the binary number 1101 to decimal.

  14. Convert the binary number 101010 to decimal.

  15. Convert the binary number 11111111 to decimal.

  16. Convert the binary number 1101001 to decimal.

  17. Convert the hexadecimal number 0 to decimal.

  18. Convert the hexadecimal number 1 to decimal.

  19. Convert the hexadecimal number 13 to decimal.

  20. Convert the hexadecimal number AB to decimal.

  21. Convert the hexadecimal number 1A2B3 to decimal.

  22. Convert the hexadecimal number 1101001 to decimal.

  23. Convert the binary number 10 to hexadecimal.

  24. Convert the binary number 1111 to hexadecimal.

  25. Convert the binary number 11001100 to hexadecimal.

  26. Convert the binary number 101010 to hexadecimal.

  27. Convert the hexadecimal number A to binary.

  28. Convert the hexadecimal number ABCD to binary.

  29. Convert the hexadecimal number 12A7F to binary.

  30. Convert the hexadecimal number 101010 to binary.