You are on page 1of 15

Number Representatio

Agenda
Intro to number representatio
Conversion between different bases (unsigned numbers
Binary additio
Signed numbers (signed magnitude vs. 2’s complement
Binary addition - Over o
Binary multiplicatio

How is Data represented in a Computer


Everything is represented with 1s and 0s
Values are physically implemented with transistors, which act as tiny switches
Those switches have 2 states: ON, OF
Those translate to 2 values (bits): 1,
In logic, that is equivalent to: True, Fals
In circuits, that would: ~5V, 0V

BIT - Binary digI

Number system everyone is familiar wit


1. Decima

Decimal - Number 6 could be represented in different ways: 6, VI, six


Base 10 (10 distinct digits): 0-9 , we can combine those digits to make other larger numbers

2. Decimal Expansio

Example: 489 - Let’s break it apart (places are powers of 10

489 = 4 * 10^2 + 8 * 10^1 + 9*10^

489 *10 = 4890 (left shift / add 0 at the end

We would work it the other way around by dividing by 10 and considering the remainders

1
l


fl
w

Representing Natural Numbers in Binar


1. Converting from Binary to Decima

Convert 100001012 to Decima


1*27 + 0*26 + 0*25 + 0 *24 + 0*23 +1*22 + 0*21+1*20 =128+4+1 = 1331

General Formula to convert to Decimal: n= akbk + ak-1bk-1+ …..+ a2b2 +a1b1+a0b0


n - Positive integer (base 10
b - Base (2 for binary, 3 for ternary, 16 for Hexadecimal, …

We could also shift and add like in decima


1 =
1 =1*2 + =
10 =2*2 + =
101 =5*2 + = 10
1010 =10*2 + =2

2. Converting from Decimal to Binar

- Preferred way of conversion from decimal: (If you’re familiar with the powers of 2, a
reasonable way to convert from decimal to binary is to subtract powers of 2 until you get 0.

Powers of 2 to remember: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 102

54: 32 is biggest, 22 left; 16 next, 6 left; 4, 2, done. 54= 32+16+4+2 => 110110
81: 64 + 16 + 1. 1010001

- The other way: Least signi cant bit to Most signi cant bit (mod by 2, which is the same as
considering the remainder after dividing by 2. Repeat till the quotient is 0
54: 0 => 27, 1 => 13, 1 => 6, 0 => 3, 1 => 1, 1 => 0 (110110
8

=>Exercise: Convert 71 to binary. Use either method.

71 = 64+4+2+
(1000111

2

0
1

1
0
1
)

0
1
0
1

1
1

fi
)

fi

3. Division Algorith
Division Algorithm Theorem: If b and n are integers with b > 0, there are unique integers q
and r such that n = qb + r and 0 <= r < b

Example: If b is 8 and n is 42, 42 = 8*5 + 2. 5 is q (the quotient), 2 is r (the remainder)

Pf: Every number clearly falls on the number line within exactly one interval [qb, (q+1)b) for
some q. [intervals b apart] The difference n - qb is smaller than b but not smaller than 0, so it
satis es the requirements for r

Theorem: Let b be an integer greater than 1. Then if n is a positive integer, n can be


expressed uniquely in the for

n = ak * b k + ak−1 * b k−1 + … + a1 * b 1 + a0 b 0

Example: Suppose our base is 7. 100 can be written uniquely a


72 * 2 + 7 * 0 + 1 * 2, or 202 7. a2 = 2,a1 = 0,a0 = 2

=> In other words, the formula works for all integer bases >= 2

Pf. (Positive numbers) By the Division algorithm, we can divide by b and get a unique
remainder for the last digit. The rest of the number is qb, which can be represented by shifting
the representation for q one place to the left. We could keep doing this until there are no digits
left to represent. Since this can produce only one number, it must be unique
(Negative numbers) The division algorithm on the absolute value can produce the correct digits,
then add a - to make the number negative again

3. Important Powers of 2s to become familiar with

• 28= 256. A chunk of 8 bits is a byte, and 256 is the number of values a byte can take on
(0-255)

• Range of values in a byte: 0-25


n−1
• Range of values in an n-bit number: 0 - 2

• 24 = 16. This is the number of values possible in half a byte, or a nibble (0-15), which will be
useful for our study of hex

• 210 = 1024. Kilobytes (~1000 bytes) don’t come up much anymore, but this is still a handy way
of estimating how big binary values are
230 = 210 * 210 * 210 = about 1,000,000,00

• Kilobytes = 210 = 1024 ~100


• Megabyte = 220 bytes ~1 Millio
• Gigabyte = 230 bytes ~1 Billio
• Terabyte = 240 bytes ~1Trillio

234 = 24 230 = 16 Gigabyt

3
fi
.

5. Binary additio

The CPU does lots of things. How could it, for instance, add numbers with logic gates
• Many things are represented in binary in computers, and T or F can be mapped to 1 and 0
• Consider a piece of the machinery that handles just two input bits in the addition
• It needs to take two bits as input and have the behavior at right for the sum and the carr

5-1 Half-Adder as an Example of Logic Circuits Performing Computation

Binary addition is similar to decimal addition (align the ones and tens
0101 (5) + 0111 (7) = 1100 (12
0011 (3) + 0011 (3) = 110 (6
1010 + 0101 = 111
00001111+00000001=0001000

Process:(a)convert the two decimal numbers to binary, (b) add the two binary numbers,
and (c) convert the result back to decimal to check the answer

* Construction of half-adder from truth table: XOR for sum bit, AND for carr

4
n

5-2 Full-Adder (Optional


Construction of full adder: adds A and B, then adds the “carry-in” bit from the previous place.
Stringing these together gives something that can add multi-bit numbers

What happens when we try to carry out and don’t have the bits? If everything represents a
positive number, over ow

6. Binary multiplicatio
Perform the following multiplications in binary. (1) convert each decimal number to binary, (2)
perform the multiplication in binary, and (3) convert the binary result back to decimal to check
your result

41×

Solution: “41 × 7 = 287”

101001
× 111
10100
101001
101001
100011111
Check Solution: 100011111 256+16+8+4+2+1 28

Observation on binary multiplication and division by 2


Shift to the left ~ multiplies by
shift to the right divides by 2

5
7

fl

Representing Natural Numbers in Hexadecima


Hexadecimal, or base 16, is also a way to talk about binary that’s more concise. It is also more
“human readable”

As a base 16 number system, each place has 16 possible digits:


0123456789ABCDEF

The number may be preceded by 0x to signal that it is hex and not binary or decimal:
0x20 = 2016 = 3210.

Range of values with n: 0 - 16n−1


Shift to the left: A3F *16 = A3F

Decimal (base 10 Binar (base 2 Hex (base 16


000
000
001
001
010
010
011
011
100
100
1 101 A
1 101 B
1 110 C
1 110
1 111 E
1 111 F

1. Converting to and from Hex


The evaluation of a particular hex string as a number is therefore a sum of powers of 16
0xA9 = (16*10 + 9*1) = 16910
0x103 = (162 + 3) = (256 + 3) = 25910

Interesting property: Hex digit = 4 bites = ½ byte = nibbl

6
0
1
2
3
4
5
6
7
8
9
1
0
2
3
4
5
.

)
1
1
0
1
0
1
0
1
0
1
0
0
1
0
1
0
y
0

)
0

The convenient thing for CS is that we can very easily convert between binary and hex
• Every 4 bits can be directly read as a hex digit, and this will be the correct hexadecimal
number
• For example, 010100102 can be read as
0101 = 5 and 0010 = 2 so the hex should be 0x52
Check: 01010010 = 64 + 16 + 2 = 8210.
0x52 = 5*16+2 = 8210
• 111111002 can be read as
1111 1100 = (15,12) = 0xFC
Check: 111111002 = 128 + 64 + 32 + 16 + 8 + 4 = 2521
0xFC = 15*16+12=25210

2. Why can we group 4 bits to form Hexadecimal digit


The largest digit in Hex is F, which corresponds to 15, which is equivalent to 1111 in binary. That
is we need maximum 4 bits to represent any hexadecimal number

Another way to think about it


Consider: 1111 1101 100
= 1111 0000 000
0000 1101 000
0000 0000 100
__________________
1111 1101 100
Shifted 8 times Shifted 4 time Not shifte

(1111) * 28 + (1101) * 24 + (1001) * 2


(15) * 162 + (13) * 161 + (1001) * 16
F D9

3. Some Examples of Hex Values in the Wild


- IP addresses are 32-bit binary numbers, most commonly expressed in the dotted-decimal
format in which the 32 bits are grouped into four bytes of 8 bits each, separated by the dot
symbol, and each byte is written out in decimal form. Thus, the following IP address, written
in binary: 10000001000010100111010011001000
is written as follows in the dotted decimal notation: 129.10.116.200
In Hex: (Check the assignment
- IPv6 IP address(network addresses):128 bits in hex 2001:db8:85a3:8d3:1319:8a2e:370:734
- Windows “blue screen of death” errors

7
=
=
0

+
+


:


s
.

- Programming languages often refer to memory addresses using HE


- Representing colors in RGB (red,green,blue): example (FF,00,00) is a representation of the
red color. Check this out as a reference

=>Exercise Convert 110010102 to hexadecimal. Answer: CA


Convert 0x61 to Decimal Answer: 0x61 = 16*6+1 = 9710

What about Other bases


Let’s consider base3. places worth remembering: 1, 3, 9, 27, 8

8 in base 3: 2 3’s plus 2 1’s = 223


92 in base 3: 81 + 9 + 2 = 101023
92 + 8 in base 3
1010
+2
———
1020

Check the result of the sum 92+8: 81 + 18 + 1 = 10

Now, what if we wanted to convert from base 3 to base 9


For base 3, the possible digits are 0, 1,
For base 9, the possible digits are 0, 1, 2, 3, 4, 5, 6, 7,

Base Base
0
0
0
1
1
1
2
2
2

Approach 1: Convert from base 3 to decimal , then from decimal to base


Approach 2: We notice that 9 = 32, meaning that we need to map any digit in base 9 to 2 digits
in base 3.
Let’s convert 1110010013 to base
We rst form groups of 2 from right to left 01 11 00 10 013
Then we convert each group to base
013 = 19
103 = 39
003 = 09
113 = 4
013 = 19

Therefore, 1110010013 = 140319

8
1
0
1
2
0
2
0
1
2
fi
2

3
1

:
0

This is similar to the approach we used to convert from base 2 to base 1


16 = 24, notice the exponent here is
We formed groups of 4 bits and converted them to 1 “digit / character” in base 16

Representing Negative Number


We have only discussed representing natural numbers in binary. For example, we can
represent 255 with 8 bits (one byte) as 1111 11112 (Unsigned binary string). What about
negative numbers

1. Signed Magnitude (8 bits)

42 = 32 + 8 +2 0010101

-42 = 1010101

79 = 64 + 8 + 4 + 2 + 1 = 0100111

-79 = 1100111

The range for 8 bits is: -127 = 11111111 to 127 = 0111111

0 = 00000000 = 1000000

Problems with this representation


1- 0 is represented as 0000 0000 and 1000 000
One representation is wasted

2- Math does not always work


29 + (-10 0001110
+1000101
————
10100111 Incorrect sum !

9

)
?

=

0

2. Two’s Complement
Positive Numbers: The standard binary representation of the number
Negative numbers: get the binary representation of the positive number, ip the bits, and add
1

Examples:
42 = 32 + 8 +2 = 0010 1010
Binary of Positive Value Flip the bits Two’s Complemen
(Add 1 after ipping the
bits when number is
negative)

42 00101010 00101010
-42 00101010 11010101 11010110
-79 01001111 10110000 10110001
-88 01011000 10100111 10101000
To convert back, ip all the bits and add 1, remember it's negative

=>Exercise:

Give the 8-bit two’s complement representations of the following integers: 55, 83, −79, −88

55 = 32+16+4+2+1 = 00110111

83 = 64+16+2+1 = 0101001

−79 79 = 64+8+4+2+1 = 01001111 10110001

−88 88 = 64+16+8 = 01011000 10101000


Range of numbers represented

• 8-bit two’s complement: -128 <-> 127 (smallest 1000 000 largest 0111 1111

What is 1000 0000 in decimal

• In general, the range of an n-bit two’s complement representation is −2n−1 to 2n−1 − 1,


inclusive

If we list out all the positive/negative two’s complement numbers in order, you can see
how the pattern starts to emerge:

10
.

fl
fl
t

0
.

-
.

fl
)

01111111 127
0111111 126
0111110 125
...
00000010 2
00000001 1
00000000 0
11111111 -1
11111110 -2
11111101 -3
11111100 -4
11111011 -5
...
10000011 -125
10000010 -126
10000001 -127
10000000 -128

Notice how 1000 000 is now used and not wasted!

Why ipping the bits and adding 1 works

Consider +4 and –4 (using a two's 8-bit representation); that is, 0000 0100 and 1111 1100

The sum of any positive number and the negative of the same number should be 0. That is, 4 +
(-4) =

Let's try it out in binary

0000 010
+ 1111 110
(1) 0000 0000

The sum here is zero within the constraints of the number of bits used (8 bits). The (1), which is
the last carry is discarded

11
fl
0

1
0


 



 
 





:

Now, we know that if a+b=c then c-b=


so, how do we get the negative representation of -4
0000 010 (4 in binary

+ 1111 1011 (complement of 4 - ip all bits


1111 111 (Sum of 4 and its complement

0000000

4 + (the complement of 4) + 1 =
4 + [(the complement of 4) + 1] =
4 + [-4] =
Therefore, -4 = the complement of 4 +
You can try it with any other number, it always works :

=>Exercise:
-27 <=> 1110010
-35 <=> 1101110
-71 <=> 1011100
-98 <=> 1001111
-103 <=> 1001100

3- Binary Subtraction (Optional


42 = 0010101
+79 = 01001111
121 01111001 = 64 + 32 + 16 + 8 + 1 =12

79 = 0100111
-42 = 11010110
37 00100101 throw away the carry

4- Identifying over ows and limitations when doing calculations on a compute


=>Exercise:

Let’s perform the following addition in binary (assume n=4). Detect whether there is
over ow.

13-6=7 6-13=-7 -6-13=-1

12
+

fl
0

1
1


0
1

fl
1

fl
1

What about
79 = 0100111
+79 = 01001111
10011110 = -(01100001 + 1)
= -01100010 = -(64 + 32 + 2)
= -98 = 256 - 158 OVERFLO

Range of values represented in two’s complement is

−2n−1 <= RESULT <= 2n−1 − 1

Detecting Over o

1- Adding 2 positive number


0xxxxxx
0yyyyyy
There will be an over ow iff xxxxxxx + yyyyyyy > 127
Start with leading 0s and end up with a 1.
2- Adding 2 negative number
1xxxxxx
1yyyyyy
There will be an over ow iff xxxxxxx + yyyyyyy < -128 so there is no carry into the left bit.
1011 1111 -01000001 = -65
1100 0000 -00111111+1 = -01000000 = -6
0111 111

-65-64 = —129 < -128 (Resulting magnitude>7)


We started with leading 1s and end up with a 0. Adding up 2 negative numbers can’t result in a
positive number

3- Adding a negative number and a positive number (Start with one leading 0 and one leading 1)
NO OVERFLO

13
1

fl
w

fl
fl
W

4- Determining Over ow in a Circuit (Optional)

Full Adder for a 4-bit binary addition

Note: Another easy way to check for an over ow is by comparing the last 2 carries , if they are
different then there is an over ow. In other words, the carry into 1MSB != carry out of MSB

1 MSB - Most Signi cant Bit

14
fi
fl
fl
fl
If the 2 numbers you are adding are positive then the most signi cant bit (MSB) for both is 0. If
the carry in is 1, then the sum will be 1 which implies that the sum is a negative and the carry out
will be 0. Here, the carry in and carry out are different

Same thing can be observed if the numbers you are adding up are negative . In this case the the
MSB for both is a 1. If the carry in is 0, then 0+1+1= 0 with the carry out 1. This will imply that
the sum is a positive number which is not possible when you add up 2 negative numbers. Here
again, the carry in and carry out are different

Conclusion: Now that we've seen Two's complement: say you have a problem with a
program you just wrote. You are looping using a counter that is counting down from 40
to 0 and then stopping. However, the program keeps crashing. When you look at the
variable in the debugger the value is 0xFFFFFFFF. What is going on? Why is it
important to understand Two's Complement in this case?

15
.

fi

You might also like