Data Representation

ECE 109 Spring 2014
Name:__________________________________________
Problem Session 2
Overview
You will work with three number systems: Decimal (d), Binary (0b) and Hexadecimal (0x). You
will learn how to convert between these three number systems and how to perform simple
arithmetic operations including subtraction using the two's complement method.
Part 0: Basics of Positional Number Systems
Decimal
Most if not all commonly used number systems are positional systems, i.e. a number is
represented by a string of digits, where each digit position has an associated weight. The
magnitude of a number is the weighted sum of the digits.
In decimal systems, each weight is a power of 10 corresponding to the digit's position. In this
case, the base of the number system is 10 and the system is known as decimal(d). For example:
1734(d) = 1*1000 + 7*100 + 3*10 + 4*1
or, if you prefer
1734(d) = 1*103 + 7*102 + 3*101 + 4*100
A decimal point allows negative as well as positive powers of 10 to be used.
235.78(d) = 2*100 + 3*10 + 5*1 + 7*0.1 + 8*0.01
235.78(d) = 2*102 + 3*101 + 5*100 + 7*10-1 + 8*10-2
The number of possible values of each individual digit is equal to the value of the base. These
values start with 0 and go up to one less than the base. Consequently, in the case of decimal
(base 10) numbers, the digits range from 0 to 9.
Binary (unsigned)
Binary digits (referred to as bits in a computer) are the digits of a base 2 number system. Hence
the possible values for each digit are limited to 0 and 1. For example, in a binary number
system,
(0b)10111001=1*27 + 0*26 + l*25 + l*24 + l*23 + 0*22 + 0*21 + 1*20 = l85(d)
Of course, you can just omit the 0 terms to compute this number as
Jan, 2014
1
ECE 109 Spring 2014
Name:__________________________________________
(0b)10111001 = 1*27 + l*25 + l*24 + l*23 + 1*20 = l85(d)
The leftmost bit of a binary number has the largest weight and is therefore called the Most
Significant Bit (MSb). The rightmost bit is called the Least Significant Bit (LSb).
In standard number notation, leading zeros are often omitted since they contribute nothing to
the sum. So, in binary 101 and 00101 are the same, and in decimal 007 and 7 are the same.
However, in actual hardware leading zeros are stored.
Hexadecimal
Hexadecimal (or hex for short) is the positional number system where the base is l6, and hence
the digits can have one of 16 possible values.
These sixteen digits cannot be represented in total by the usual ten digits, so the six letters
from A to F are used for digits numbered from 10 to 15. The means that C as a hex digit has the
value 12.
For example,
(0x)B9 = 11*16 + 9*1 = l85(d)
Part 1: Conversion Between Systems (unsigned)
The procedure for converting a decimal number to any other number system is tedious at best.
Several successive divisions of the original number by the base of the new system are required
to convert a number.
Decimal to Binary
Assume the number we wish to convert to binary is 84(d).
84 / 2 = 42, with remainder 0 -- This is the least significant bit
42 / 2 = 21, with remainder 0
21 / 2 = 10, with remainder 1
10 / 2 = 5, with remainder 0
5 / 2 = 2, with remainder 1
2 / 2 = 1, with remainder 0
1 / 2 = 0, with remainder 1 -- This is the most significant bit
The conversion will then be: 84(d) = (0b)1010100.
Decimal to Hex
Similarly for Hex we have, assuming we wish to convert 108(d).
108 / 16 = 6, remainder 12 (least significant digit)
6 / 16 = 0, remainder 6 (most significant digit)
Jan, 2014
2
ECE 109 Spring 2014
Name:__________________________________________
Hence, the conversion will be: 108(d) = (0x)6C.
Binary to Hex, and vice versa
It is very simple to convert a binary number to hex. This is because four bits represent one hex
digit. This is also the reason for the common use of hex numbers as a shorthand for binary.
From binary to hex: (0b)1001 0100 1110 0111 = (0x)94E7
From hex to binary: (0x)6BA3 = (0b)0110 1011 1010 0011
Exercises (using unsigned positive integers)
Convert all of the following to their decimal equivalent:
(0b)11001001
(0b)10111100
(0b)01101101
(0b)10000110
Convert the following to binary first, and then to decimal:
(0x)A32F
(0x)1678
(0x)8EEF
(0x)FFFE
Show your solutions to your instructor.
Part 2: Binary Arithmetic
Addition
Binary addition is based on the same principle as decimal addition, namely that of adding
corresponding digits, and propagating the carry.
Exercise (using unsigned positive integers)
Add the following, and convert the result to decimal:
11001 + 00101
01111 + 00011
10010 + 11011
00111 + 00111
11110 + 00010
00011 + 10001
Show your solutions to your instructor.
Jan, 2014
3
ECE 109 Spring 2014
Name:__________________________________________
Representing Both Positive and Negative Integers
If you subtract 17 from 3, you'll get -14. Before looking into subtraction, we need to figure out
how to represent negative numbers, such as -14, using bits. Most computers represent signed
integers using two's complement notation. In two's complement notation, a fixed number of
bits (i.e., a word), are set aside to represent signed integers. Usually the word size is 32 or
larger; but in our examples we'll use 8, because that's 75% smaller.
In two's complement notation, the MSb is 0 for positive numbers and 1 for negative numbers.
The two's complement representation of a positive numbers is the same as the unsigned binary
representation discussed above. But in two's complement representation we must be sure to
add enough leading zeros to fill the word. So in 8-bit two's complement, 17(d) is (0b)00010001
and 3(d) is (0b)00000011.
To find the representation of a negative number, such as -17(d), start with the bits for the
absolute value of the number. In this case, that would be (0b)00010001. Next invert all those
bits to obtain the one's complement representation. Here this gives us (0b)11101110.
Now, add one to this number to obtain the two's complement representation. In this case,
(0b)11101111.
Just for fun add 17(d) (or (0b)00010001) to -17(d) (or (0b)11101111). This will give you a 9-bit
result, but you must throw away the 9th bit because it exceed the capacity of our 8-bit word.
00010001
+ 11101111
-------------00000000
17
+ -17
----0
It is comforting to get 0! Now let's try adding 3(d) to -17(d) and then add 14(d) to that result.
We better get 0 again.
00000011
+ 11101111
-------------11110010
+ 00001110
-------------00000000
3
+ -17
----??
+ 14
----0
It is worthwhile to make an important distinction here. The 8-bit two's complement
representation of 107(d) is (0b)01101011. But we can also refer to two's complement as an
operation, as in, "what's the two's complement of that number." Referring now to the two's
complement operation, the 8-bit two's complement of (0b)01101011 is (0b)10010101.
Jan, 2014
4
ECE 109 Spring 2014
Name:__________________________________________
The 8-bit two's complement representation of -107(d) is (0b)10010101. The 8-bit two's
complement (i.e., an operation) of (0b)10010101 is (0b)01101011.
Subtraction
Binary subtraction requires a minuend, the number we're subtracted from, and a subtrahend.
In computers this is done by adding the minuend to the two's complement (the operation) of
the subtrahend. The subtraction then follows the rules of a simple binary addition,
though you do discard that overflow-bit.
Suppose you are going to compute 100(d) - (-1(d)) in a system that uses 8-bit two's complement
representation. The two's complement representation of 100(d) is (0b)01101000 and the two's
complement representation of -1(d) is (0b)11111111. (Remember, that one. No matter how
many bits are in a word; in two's complement representation, when all the bits are 1, the word
represents -1.) The two's complement (the operation) of (0b)11111111 is (0b)00000001.
(0b)01101000 + (0b)00000001 is (0b)01101001, or 101(d).
Exercises (using 2s complement representation)
Express the following numbers is 8-bit twos complement representation.
-30
35
-128
Give the two's complement (the operation) of the following 8-bit binary numbers.
01001111
11111000
10000000
Carry out the following subtractions by adding the minuend to the
two's complement of the subtrahend.
011001 - 000011
111011 - 011100
000100 - 010111
000111 - 110011
001110 - 010001
Show your solutions to your instructor.
Jan, 2014
5
ECE 109 Spring 2014
Name:__________________________________________
Part 3: Two's Complement Overflow
Arithmetic overflow occurs when the magnitude of the numeric value to be stored is greater
than that which can be represented in the number of bits provided.
In an unsigned binary representation, the maximum numeric value that can be represented in N
bits is 2N-1. For example:
8 bits: maximum representable value 28-1 = 255
16 bits: maximum representable value 216-1 = 65,535
32 bits: maximum representable value 232-1 = 4,294,967,295 (the most common width for
personal computers as of 2005),
64 bits: maximum representable value 264-1 = 18,446,744,073,709,551,615
128 bits: maximum representable value 2128-1 =
340,282,366,920,938,463,463,374,607,431,768,211,455
In an unsigned binary representation, overflow occurs when there is a carry out of the MSb.
In a two's complement representation, the values expressed in N bits must include both
positive and negative numbers. An N-bit two's-complement representation can express every
integer in the range
-2N-1 to 2N-1 -1.
The rules for detecting overflow in a two's complement representation during addition are
simple:
•
•
•
If the sum of two positive numbers yields a negative result, the sum has overflowed.
If the sum of two negative numbers yields a positive result, the sum has overflowed.
Otherwise, the sum has not overflowed.
In an unsigned binary representation, a carry out of the MSb is equivalent to overflow. In a
two's complement representation, a carry out of the MSB does not identify overflow. Overflow
in a two's complement representation occurs when the carry into the MSb is different than
the carry out of the MSb.
A negative number added to a positive number cannot create overflow, because their sum is
between the addends. Since both of the addends fit within the allowable range of numbers,
and their sum is between them, it must fit as well.
Exercises (using 2s complement representation)
What is the largest number that can be represented in 10-bit two's complement
representation, what is the smallest?
Jan, 2014
6
ECE 109 Spring 2014
Name:__________________________________________
Identify which of the additions below result in overflow.
0101011010
+1001000101
0101011010
+0011000110
Show your answers to your instructor and go home.
Jan, 2014
7