You are on page 1of 13

Decimal System Basics

- The decimal system:


o Uses Base 10
o If each value to be represented requires only 1 digits, then we need 10 digits to
represent 10 different values
o Digits range from 0 till 9
o To represent values greater than 10, we need to use more digits. These digits are
written next to each other and interpreted in a way to reach higher values. For example,
the number one hundred and seventy five needs 3 digits ①⑦⑤. These digits are
placed in a table where each column has a weight. This weight is calculated as a power
of 10 based on the position of the column. The rightmost column is position 0 and as we
move to the left we increase the positions

Position 2 1 0
weight 102 = 100 101 = 10 100 = 1

Hence, the above number ①⑦⑤ will be placed in the table starting from the right
and moving leftwards.

Position 2 1 0
weight 102 = 100 101 = 10 100 = 1
Number ① ⑦ ⑤

To obtain the final value of the above number: Each digit is multiplied by the
corresponding weight of its column, then they are all added together 
① * 100 + ⑦ * 10 + ⑤ * 1 = 100 + 70 + 5 = 175

Abstracting from Base 10 to any other Base


- Base N (where N<10):
o If each value to be represented requires only 1 digits, then we need N digits to represent
N different values
o Digits range from 0 till (N-1)
o To represent values greater than N, we need to use more digits. These digits are written
next to each other and interpreted in a way to reach higher values

Position 2 1 0
weight N2 N1 N0 = 1

o To obtain the final value of any number in the table, you multiply each digit by its weight
and add them all up
Base 2 (N = 2)
- Base 2:
o If each value to be represented requires only 1 digits, then we need 2 digits to represent
2 different values
o Digits range from 0 till 1 (0 and 1)
o To represent values greater than 2, we need to use more digits. These digits are written
next to each other and interpreted in a way to reach higher values

Position 2 1 0
weight 22=4 21=2 20 = 1
Number 0 1 1

o To obtain the final value of any number in the table, you multiply each digit by its weight
and add them all up. Final value = 0*4 + 1*2 + 1*1 = 0 + 2 + 1 = 3

How Computers Represent Data


Computers are made out of electronic components, which can have one of 2 states (ON/OFF,
True/False, Black/White, Yes/No). Hence, the most convenient numbering system is the binary system,
which consists of only 2 digits (0 and 1)

Some Definitions

- A Binary Digit is referred to as a bit


- With only 1 bit, we cannot represent a lot of information. Hence, in order to work with more
complex information, we need to group several bits together. The basic grouping of bits was the
Byte where 1 Byte = 8 bits. For example, if we say we have 5 Bytes, this means that we actually
have 5*8=40 bits.
- Like in any measurement system, we need to scale up to reach much higher values and use
notations to refer to Thousand, million, billion,…
o 1 KB (kilobyte) = 1,000 Bytes = 103 Bytes
o 1 MB (Megabyte) = 1,000,000 Bytes = 106 bytes
o 1 GB (gigabyte) = 1,000,000,000 Bytes = 109 bytes
o 1 TB (terabyte) = 1,000,000,000,000 Bytes = 1012 bytes
- The above scaling units are meant to simplify the calculation needed to determine the actual
number of bits involved. For example, 3 GB = 3 * 109 bytes = 3 * 109 * 8 bits = 24,000,000,000
bits = 24 Gb
o Ex1: 4 MB = ? bits = 4* 106 bytes = 4* 106 *8 bits = 32 * 106 bits = 32 Mb
o Ex2: 16 Kb = ? B = 16 * 103 bits = 16 * 103 /8 bytes = 2 * 103 bytes = 2 KB
- However, to be more accurate, 1 KB = 210 bytes = 1,024 bytes. The reason for this is that 1000 is
not an actual power of 2. The closest power of 2 to reach 1,000 is 210. And as we scale up the
units, we multiple by 210, so that:
o 1 KB (kilobyte) = 210 Bytes = 1,024 Bytes
o 1 MB (Megabyte) = 220 Bytes = 1,048,576 bytes
o 1 GB (gigabyte) = 230 Bytes = 1,073,741,824 bytes
o 1 TB (terabyte) = 240 Bytes = 1,099,511,627,776 bytes
- Even though the above calculation is more accurate, it brings complexity to calculating the
overall number of bits involved, while the difference in the final number of bits is considered
minimal.
So, repeating the example 3 GB = 3 * 230 bytes = 3 * 230 * 8 bits = 25,769,803,776 rather than
24,000,000,000 bits
- In communication, data transfer rate, which refers to the speed of the connection, is measured
in bits per second, using the notation bps. Please note the use of lowercase b (b=bit). This is
because cables transmit one bit at a time at a very fast rate

Data storage, such as memory capacity, is measured in Bytes, such as GB. Please note the use of
uppercase B (B=Byte). This is because, storage devices work with blocks of 8 bits at a time rather than
only 1 bit

Positive Binary Integers


- Even though computers work with the binary system, we need to convert binary values into
their decimal equivalent, so that we as humans can interpret these numbers.
- The simplest is to work with positive binary integers:
o Positive: All numbers are greater than zero
o Integers: No decimal points. Whole numbers.
- Notation when converting from one numbering system to another: For example to convert from
base x to base y we write: (digits in base x)x = (digits in base y)y
- For example, to convert from base 2 to base 10, we use the notation: (bits)2 = (decimal digits)10

Using 2 bits

Example 1: Convert (10)2 to decimal:

Position 1 0
weight 21=2 20 = 1
Number 1 0
1*2 + 0*1 = 2+0=2  (10)2 = (2)10

Example 2: Convert (01)2 to decimal:

Position 1 0
weight 21=2 20 = 1
Number 0 1
0*2 + 1*1 = 0+1=1  (01)2=(1)10

In binary, we use only 0 or 1:

- When we multiply 0 by a power of 2, we get 0  If, in a bit position, we use 0, then we just skip
passed the corresponding power of 2
- When we multiply 1 by a power of 2, we get the corresponding power of 2 value  if, in a bit
position we use 1, then we will use the corresponding power of 2 in the addition to obtain the
equivalent decimal number
- As a conclusion, when converting from binary to decimal:
o We add the powers of 2 (weight) where we have 1
o We ignore the powers of 2 (weight) where we have 0

Using 3 bits

Example 3: Convert (010)2 to decimal:

Position 2 1 0
2
weight 2 =4 21=2 20 = 1
Number 0 1 0
0*4+1*2+0*1=0+2+0=2  (010)2=(2)10

Example 4: Convert (101)2 to decimal:

Position 2 1 0
Weight 22=4 21=2 20 = 1
Number 1 0 1
4+1=5  (101)2=(5)10

Example 5: Convert (111)2 to decimal:

Position 2 1 0
Weight 22=4 21=2 20 = 1
Number 1 1 1
4+2+1=7  (111)2=(7)10

Using 4 bits

Example 6: Convert (1001)2 to decimal:

Position 3 2 1 0
weight 23=8 22=4 21=2 20 = 1
Number 1 0 0 1
8+1=9  (1001)2=(9)10

Using 8 bits

Example 7: Convert (10011010)2 to decimal:

Position 7 6 5 4 3 2 1 0
weight 27=128 26=64 25=32 24=16 23=8 22=4 21=2 20 = 1
Number 1 0 0 1 1 0 1 0
128+16+8+2=154  (10011010)2=(154)10

Example 8: Convert (10100101)2 to decimal:


Position 7 6 5 4 3 2 1 0
Weight 2 =128 26=64
7
25=32 24=16 23=8 22=4 21=2 20 = 1
Number 1 0 1 0 0 1 0 1
128+32+4+1=165  (10100101)2=(165)10

Lowest Value using 8 bits: (00000000)2=(0)10

Highest Value using 8 bits: (11111111)2=128+64+32+16+8+4+2+1=(255)10

Convert from Decimal to Binary


Example 1: (154)10=(10011010)2

To convert from decimal to binary, we divide the number by 2 and record the remainder. We continue
until we reach either 0 or 1 at the end:

154 /2 = 77 Remainder = 0 (right)

77 / 2 = 38 Remainder = 1

38 / 2 = 19 Remainder = 0

19 /2 = 9 Remainder = 1

9/2=4 Remainder = 1

4 /2 = 2 Remainder = 0

2/2=1 Remainder = 0

1/2=0 Remainder = 1 (left)

10011010

Example 2: (64)10=(100000)2

64 / 2 = 32 Remainder = 0 (Right)

32 / 2 = 16 Remainder = 0

16 / 2 = 8 Remainder = 0

8/2=4 Remainder = 0

4/2=2 Remainder = 0

2/2=1 Remainder = 0

1/2=0 Remainder = 1 (left)

(64)10=(1000000)2

Example 3: (25)10=(11001)2

25 / 2 = 12 Remainder = 1 (Right)
12 / 2 = 6 Remainder = 0

6/2=3 Remainder = 0

3/2=1 Remainder = 1

1/2=0 Remainder = 1 (Left)

(25)10=(11001)2

To represent number (25)10 in binary, we only needed 5 bits. But sometimes, we might be asked to write
this number using 8 bits. To do so, we simply add 0s to the left. Using 8 bits: (25)10=(00011001)2

75 / 2 = 37 Remainder = 1

37 / 2 = 18 Remainder = 1

18 /2 = 9 Remainder = 0

9/2=4 Remainder = 1

4 /2 = 2 Remainder = 0

2/2=1 Remainder = 0

1/2=0 Remainder = 1 (left)

01001011

Position 7 6 5 4 3 2 1 0
weight 128 64 32 16 8 4 2 1
0 1 0 0 1 0 1 1

Total Number of Values Using x bits


- Using 2 bits, how many different values can we get?

0 0
0 1
1 0
1 1
With 2 bits, we can get 4 different values (22=4)

- Using 3 bits, how many different values can we get?

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
With 3 bits, we can get 8 different values (23=8)

- Using x bits, the total number of different values we can get is 2 x

With x=8, the total number of different values = 28=256


Lowest value = 0
Highest value = 255 (28-1)

In summary, using x bits (positive binary integers)


- We can have a maximum of 2x different values
- Lowest value = 0
- Highest value = 2x-1

Hexadecimal
Hexadecimal, also referred to as Hex  Base 16

Every digit should represent one of 16 different values. We need 16 digits in total, where each digit will
have a value from 0 to 15

Using normal number digits, we can represent 10 values: 0  9

We now need 1 digit to represent the values of 10, 11, 12, 13, 14, and 15. So we will rely on the first 6
alphabet letters where each letter will be assigned for a Hex value:

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

Convert from Hex to Decimal:

Using 2 Hex digits:

Example 1: (37)16

Position 1 0
weight 161=16 160 = 1
Number 3 7

3*16+7*1= 48+7 = 55

(37)16=(55)10

To make calculation easier, we will use the binary system as an intermediate numbering system to
transition from Hex to Decimal and vice versa.
Convert from Hex to Binary:

- Every Hex digit, needs 4 bits to be represented in binary

8 4 2 1

- Binary representation of each Hex Digits


o (0)16 = (0000)2
o (1)16 = (0001)2 [1]
o (2)16 = (0010)2 [2]
o (3)16 = (0011)2 [2+1]
o (4)16 = (0100)2 [4]
o (5)16 = (0101)2 [4+1]
o (6)16 = (0110)2 [4+2]
o (7)16 = (0111)2 [4+2+1]
o (8)16 = (1000)2 [8]
o (9)16 = (1001)2 [8+1]
o (A)16 = (1010)2 [10=8+2]
o (B)16 = (1011)2 [11=8+2+1]
o (C)16 = (1100)2 [8+4]
o (D)16 = (1101)2 [8+4+1]
o (E)16 = (1110)2 [8+4+2]
o (F)16 = (1111)2 [8+4+2+1]

(37)16=(0011 0111)2=(55)10

Convert from Hex to Decimal Using Binary:

- We proceed one Hex digit at a time starting from the leftmost Hex digit
- Convert each Hex digit to binary
- Group the resultant bits together
- Convert the resultant binary number to decimal

Example 1: (37)16=(0011 0111)2= (55)10

3  2+1 = 0011

7  4+2+1=0111

(37)16 = (0011 0111)2

(0011 0111)2 = 32+16+4+2+1 = (55)10

Convert from binary to Hex. If we have less than 4 bits for each hex digits, we just add 0 to the left

(101 1100)2=(0101 1100)2 = (5C)16


Binary Addition
0+0 =0

0+1=1

1+0=1

1+1=0 Carry 1  10

Example 1:

2+6=8

Let us do the addition in binary.

1. Write first number in binary: (2)10=(10)2


2. Write the 2nd number in binary: (6)10=(110)2
3. Since the 2nd number is 3 bits, we will also write the first number using 3 bits by adding a 0 to
the left. (2)10=(010)2
4. Do the addition in binary

1 1
0 1 0
1 1 0
1 0 0 0

5. Write the result in decimal: (1000)2=(8)10

Example 2:

6+5=11

Let us do the addition in binary.

1. Write first number in binary: (6)10=(110)2


2. Write the 2nd number in binary: (5)10=(101)2
3. Do the addition in binary

1
1 1 0
1 0 1
1 0 1 1

4. Write the result in decimal: (1011)2=(11)10=(B)16

Example 3:
6+7=13

Let us do the addition in binary.

1. Write first number in binary: (6)10=(110)2


2. Write the 2nd number in binary: (7)10=(111)2
3. Do the addition in binary

1 1
1 1 0
1 1 1
1 1 0 1

4. Write the result in decimal: (1011)2=(11)10=(B)16

Negative Integers: Two’s Complement Notation


Using Two’s complement notation, positive integers should have the leftmost bit as 0. If the leftmost
bit is 1, the number will be interpreted as a negative number

When using x bits to represent an integer using 2’s complement notation, the leftmost bit is used as a
sign bit. Hence, calculation to convert from binary to decimal is done using the remaining (x-1) bits. As
an example, if we use 4 bits to represent integers using two’s complement notation, the leftmost bit is
the sign bit and calculation is done using the remaining 3 bits.

 As a result, the largest positive integer is: 0111 = +7. With two’s complement notation, the
largest positive number is no longer 1111=15. Now it is 0111= +7. Range of positive integers:
07, totaling 8 different values.
 The remaining 8 values are the negative numbers. They will range from -1 to -8.
 Now, using two’s complement notation with 4 bits, we can represent 24=16 different numbers
that range from -8 till +7 (-8 till -1 are the negative numbers and 0 till +7 are the positive
numbers)

Converting from Decimal to Binary

Example 1:

(-5)10

1. Convert the positive part to binary: (+5)10=(0101)2.


2. To get the negative part of the number, we do the following:
a. Invert (opposite) all bits of the obtained binary sequence: 0101  1010
b. Add 1  1010+1 = 1011. This is the negative representation of the above number

(-5)10=(1011)2 using Two’s complement

Example 2:

(-14)10

1. Convert the positive part to binary: (+14)10=(01110)2.


2. To get the negative part of the number, we do the following:
a. Invert (opposite) all bits of the obtained binary sequence: 01110  10001
b. Add 1  10001+1 = 10010. This is the negative representation of the above number

(-14)10=(10010)2 using Two’s complement


A faster way of converting a negative number from decimal to binary:

1. Convert the positive part to binary: (+14)10=(01110)2.


2. Start from the right. Copy all bits as they are until you reach the first 1. Then invert (flip) all other
bits
3. Convert the positive part to binary: (+14)10=(01110)2.
4. Start from the right. Copy all bits as they are until you reach the first 1. Then invert (flip) all other
bits
Positive (+14) 0 1 1 1 0
Negative (-14) 1 0 0 1 0
Invert. Invert Invert First 1  same. Start Here.
Leftmost Invert all others 0  same
bit 1  after this bit
Negative

Converting from Binary to Decimal

Example 1:

(1011)2=(-5)10

1. Since the leftmost bit is 1, then this is a negative number


2. Write the equivalent positive number in binary, using the same technique as we did when we
obtained the equivalent negative binary sequence from its positive sequence. Start reading the
bits from the right, we keep the same bits until we reach the first 1, then we write the inverse
(we flip) all other bits to the left
1011  0101
3. Convert the obtained positive binary sequence to decimal using the standard techniques:
(0101)2=4+1=(5)10
4. The result is obtained by adding negative sign to the decimal number  -5

Example 1:

(11110010)2=(-14)10

1. Since the leftmost bit is 1, then this is a negative number


2. Write the equivalent positive number in binary, using the same technique as we did when we
obtained the equivalent negative binary sequence from its positive sequence. Start reading the
bits from the right, we keep the same bits until we reach the first 1, then we write the inverse
(we flip) all other bits to the left
11110010  00001110
3. Convert the obtained positive binary sequence to decimal using the standard techniques:
(00001110)2=8+4+2=(14)10
4. The result is obtained by adding negative sign to the decimal number  -14

Representing Characters: ASCII Codes


American Standard Code for Information Interchange (ASCII) is the most popular code set used to
represent characters using 7 bits. Each character has a unique binary notation. We can represent a max
of 27=128 characters

ASCII can represent characters for only the English language. Relying on ASCII alone made exchanging
information from different languages quite impossible.

Unicode is another way to represent characters. It uses 16 bits for each characters. With Unicode,
computer systems are able to represent characters from all human languages using a single very large
table, making exchanging information from different languages much easier nowadays

With ASCII, we will write 8 bits for every character, keeping the leftmost bit 0.

- All lowercase characters start with 011 from the left


- All uppercase characters start with 010 from the left
- The lowercase and uppercase characters for the same letter have the same remaining 5 bits

If you are given the ASCII code for a character, you can generate the equivalent ASCII code for any other
character as long as you know the relationship between the 2 characters

Example

Given that “c” is (099)10, find the ASCII code for “E”

1. c=(099)10=(01100011)2
2. e=c+2
3. e=(99+2)10=(101)10=(01100101)2. It is best to convert the binary representation to decimal and
do the calculation (addition or subtraction) in decimal, then convert the obtained decimal
number to binary
1. Having obtained the representation of “e” in binary, write the binary representation of “E”. by
changing the 3 leftmost bits from 011 to 010: E=(01000101)2
2. Convert the binary representation of E to decimal: E=(01000101)2=(69)10

You might also like