Documente Academic
Documente Profesional
Documente Cultură
Contents
[hide]
• 1 Sign-and-magnitude method
• 2 Ones' complement
• 3 Two's complement
• 4 Excess-n
○ 4.1 See also
• 5 Base −2
• 6 Comparison table
• 7 See also
• 8 References
[edit] Excess-n
Main article: Offset binary
8 bit excess-127
Binary Excess-127 Unsigned
value interpretation interpretation
00000000 -127 0
00000001 -126 1
... ... ...
01111111 0 127
10000000 1 128
... ... ...
11111111 +128 255
Excess-N, also called biased representation, uses a pre-specified number N as a biasing value. A
value is represented by the unsigned number which is N greater than the intended value. Thus 0
is represented by N, and −N is represented by the all-zeros bit pattern.
This is a representation that is now primarily used for the exponent of floating-point numbers.
The IEEE floating-point standard defines the exponent field of a single-precision (32-bit) number
as an 8-bit excess-127 field. The double-precision (64-bit) exponent field is an 11-bit excess-
1023 field.
[edit] See also
• Excess-3
• Two's complement
[edit] Base −2
In conventional binary number systems, the base, or radix, is 2; thus the rightmost bit represents
20, the next bit represents 21, the next bit 22, and so on. However, a binary number system with
base −2 is also possible. The rightmost bit represents (−2)0=+1, the next bit represents (−2)1=−2,
the next bit (−2)2=+4 and so on, with alternating sign. The numbers that can be represented with
four bits are shown in the comparison table below.
The range of numbers that can be represented is asymmetric. If the word has an even number of
bits, the magnitude of the largest negative number that can be represented is twice as large as the
largest positive number that can be represented, and vice versa if the word has an odd number of
bits.
[edit] Comparison table
The following table shows the positive and negative integers that can be represented using 4 bits.
4 bit integer representations
Sign and Ones' Two's Excess-7
Decimal Unsigned Base −2
magnitude complement complement (biased)
+16 N/A N/A N/A N/A N/A N/A
+15 1111 N/A N/A N/A N/A N/A
+14 1110 N/A N/A N/A N/A N/A
+13 1101 N/A N/A N/A N/A N/A
+12 1100 N/A N/A N/A N/A N/A
+11 1011 N/A N/A N/A N/A N/A
+10 1010 N/A N/A N/A N/A N/A
+9 1001 N/A N/A N/A N/A N/A
+8 1000 N/A N/A N/A 1111 N/A
+7 0111 0111 0111 0111 1110 N/A
+6 0110 0110 0110 0110 1101 N/A
+5 0101 0101 0101 0101 1100 0101
+4 0100 0100 0100 0100 1011 0100
+3 0011 0011 0011 0011 1010 0111
+2 0010 0010 0010 0010 1001 0110
+1 0001 0001 0001 0001 1000 0001
+0 N/A 0000 0000 N/A N/A N/A
0 0000 N/A N/A 0000 0111 0000
−0 N/A 1000 1111 N/A N/A N/A
−1 N/A 1001 1110 1111 0110 0011
−2 N/A 1010 1101 1110 0101 0010
−3 N/A 1011 1100 1101 0100 1101
−4 N/A 1100 1011 1100 0011 1100
−5 N/A 1101 1010 1011 0010 1111
−6 N/A 1110 1001 1010 0001 1110
−7 N/A 1111 1000 1001 0000 1001
−8 N/A N/A N/A 1000 N/A 1000
−9 N/A N/A N/A N/A N/A 1011
−10 N/A N/A N/A N/A N/A 1010
−11 N/A N/A N/A N/A N/A N/A
Negative Numbers and Binary Subtraction
We have seen how simple logic gates can perform the process of binary addition. It is only
logical to assume that a similar circuit could perform binary subtraction.
If we look at the possibilites involved in subtracting one 1-bit number from another, we can
quickly see that three of the four possible combinations are easy and straight-forward. The fourth
one involves a bit more:
0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 1, with a borrow bit.
That borrow bit is just like a borrow in decimal subtraction: it subtracts from the next higher
order of magnitude in the overall number. Let's see what the truth table looks like.
INPUTS OUTPUTSThis is an interesting result. The difference, A-B, is still an
A - Exclusive-OR function, just as the sum was for addition. The
A B BORROW
B borrow is still an AND function, but is A'B instead of AB.
0 0 0 0 What we'd like to do, now, is find an easy way to use the binary
adder to perform subtraction as well. We already have half of it
0 1 1 1 working: the difference output. Can we simply invert the A input
so the AND gate will have the right signals? No, we can't, because
1 0 0 1 that would invert the sense of the Exclusive-OR function.
What would be really nice is to convert B to the negative
equivalent of its value, and then use the basic adder just as it
1 1 0 0 stands. To see if we can do that, let's consider negative binary
numbers below.
As we discovered when looking at binary counters, once a full Binary Unsigned Signed
count is obtained, the next clock pulse will cause the counter to Decimal Decimal
read zero again. Likewise if we set up a counter to count 0000 0 0
backwards, the first clock pulse will cause the count to go from
all zeroes to all ones. Thinking along these lines, we can see that 0001 1 1
the binary number 1111 might represent the decimal number 15, 0010 2 2
or it could represent the number -1.
On the right is the counting sequence for a 4-bit binary number, 0011 3 3
with decimal equivalents expressed in two ways. First we have 0100 4 4
the unsigned counting sequence, where all numbers are assumed
to be positive. Then we see the signed sequence, which includes 0101 5 5
both positive and negative numbers.
0110 6 6
Looking at the two decimal counting sequences, we note two 0111 7 7
factors right away: 1000 8 -8
1. The positive signed numbers are the same as their 1001 9 -7
unsigned counterparts.
2. Negative signed numbers all correspond to the most 1010 10 -6
significant bit of the binary number being a logic 1. 1011 11 -5
Because positive numbers are the same in both sequences, they
1100 12 -4
can be used together without difficulty. We only need to keep
track of how we want to define the system. And the fact that 1101 13 -3
negative numbers all have the binary MSB = 1 is helpful because
the MSB can immediately be used to identify the sign of the 1110 14 -2
number. Indeed, the binary MSB is commonly known as the sign
bit. The use of this bit to distinguish between positive and
negative numbers also allows us to divide the counting sequence
evenly between positive and negative numbers.
Now we need to look at the relationship between the binary
numbers for positive and negative versions of the same
magnitude. Suppose we invert each bit of 0001 (+1) to get 1110
(-2). If we then increment the result, we get 1111 (-1), which is
what we wanted. Will this relationship hold for all negative
numbers?
In fact, it does work, as you can determine for yourself. To form
the negative of any number, first complement all bits of the
number. The result is the one's complement of the original
number. Then, add 1 to the result, thus forming the two's 1111 15 -1
complement of the original number. Arithmetic involving such
signed numbers is known generally as two's complement
arithmetic.
To check the validity of this process, let's take the two's
complement of 0. We should logically get a result of 0. So, we
start with 0000, and form the one's complement (1111). Now add
1 to the result (10000). But this won't fit in a 4-bit number, so the
extra 1-bit is lost, leaving a result of (0000). Sure enough, -0 = 0,
as it should. Remember to discard the carry from the highest-
order bit. Two's complement arithmetic always works this way.
Note: It is not possible to represent +8 as a 4-bit signed number.
Therefore it is not possible to correctly take the two's
complement of -8. It will come back again as -8.
Now that we have an easy way to obtain the negative of any number, we can convert our original
4-bit adder circuit to an adder/subtractor. By leaving the inputs unchanged, we get the result of A
+ B. But if we invert B and add 1 with the low-order Cin, we get the result of A - B.
We can use Exclusive-OR gates, as shown to the right, to control whether we will add or subtract
on any given occasion. With a control input of 0, the XOR gates will leave the B input number
unchanged, and will also apply a logic 0 as the initial input carry. This is exactly what we want
in order to add the two numbers. However, if we apply a logic 1 to the control input, the XOR
gates will invert the B input number to form its one's complement, and will also add 1 through
the initial input carry. This changes B to its two's complement. Thus, the output result will
actually be A - B. (Note that in two's complement addition, the output carry is ignored. You can
also think of it as an inverted "borrow" bit rather than as a carry, so that a carry of 1 corresponds
to a borrow of 0. That logic also holds for the input carry, which also represents an input borrow
bit of 0.)
When we add or subtract signed numbers, we need to introduce a new concept: overflow.
Overflow occurs when the result has the wrong sign bit for the operation that was performed. For
example, if we add two positive numbers (7 and 6), we should get a positive result (13).
However, using 4-bit binary numbers, we would add 0111 to 0110 and get 1101 as the result. In
signed notation, this is a result of -3, not +13. Therefore, an overflow has occurred, where the
result would have to have more bits than the original two numbers.
This is not as much of a problem as you might think. An 8-bit number can have signed values in
the range -128 to +127. A 16-bit signed number may hold any value from -32,768 to +32,767.
These ranges are sufficient for most practical applications. Where they are not, modern
computers can easily use 32-bit numbers (±2.14 × 109) or 64-bit numbers (±9.22 × 1018) for the
purpose.
If we add a positive number to a negative number, overflow cannot occur. Likewise, if we are
subtracting two numbers of the same sign, overflow is impossible. But if we add like-signed
numbers or subtract unlike-signed numbers, we must be aware of the possibility of overflow, and
recognize when it occurs.
Modern microprocessors are designed to recognize and report when overflow occurs in any
arithmetic operation.

Top of Form
Representation of Numbers
Table of Contents
• Introduction (the decimal number system)
• Binary representation of positive integers
• Hexadecimal, Octal, Bits, Bytes and Words
• Signed binary integers (2's complement)
• Positive binary fractions
• Signed binary fractions
• A Quicker Method for Converting Binary Fractions
• Floating point numbers
• IEEE single precision floating point, 32 bits (ANSI/IEEE Standard 743-1985)
• IEEE double precision floating point
Introduction
When working with any kind of digital electronics in which numbers are being represented, it is
important to understand the different ways numbers are represented in these systems. Almost
without fail, numbers are represented by two voltage levels which can represent a one or a zero
(an interesting exception to this rule is the new memory device recently announced by Intel
which uses one of four possible voltage levels, thereby increasing the amount of information that
can be stored in a given space). The number system based on ones and zeroes is called the binary
system (because there are only two possible digits). Before discussing the binary system, a
review of the decimal (ten possible digits) system is in order, because many of the concepts of
the binary system will be easier to understand when introduced alongside their decimal
counterpart.
You should all have some familiarity with the decimal system. For instance, to represent the
positive integer one hundred and twenty-five as a decimal number, we can write (with the
postivie sign implied). The subscript 10 denotes the number as a base 10 (decimal) number.
12510 = 1*100 + 2*10 + 5*1 = 1*102 + 2*101 + 5*100
The rightmost digit is multiplied by 100, the next digit to the left is multiplied by 101, and so on.
Each digit to the left has a multiplier that is 10 times the previous digit. Hopefully this is all a
review. Some observations:
• To multiply a number by 10 you can simply shift it to the left by one digit, and fill
in the rightmost digit with a 0 (moving the decimal place one to the right). To
divide a number by 10, simply shift the number to the right by one digit (moving
the decimal place one to the left).
• To see how many digits a number needs, you can simply take the logarithm (base
10) of the absolute value of the number, and add 1 to it. The integer part of the
result is the number of digits. For instance,
log 10(33) + 1 = 2.5.
The integer part of that is 2, so 2 digits are needed.
• With n digits, 10n unique numbers (from 0 to 10n-1) can be represented. If n=3,
1000 (=103) numbers can be represented 0-999.
• Negative numbers are handled easily by simply putting a minus sign (-) in front of
the number. This does lead, however, to the somewhat awkward situation where
0=-0. We will avoid this situation with binary representations, but with a little bit
of effort.
or
8610 = 1010110 2
The subscript 2 denotes a binary number. Each digit in a binary number is called a bit. The
number 1010110 is represented by 7 bits. Any number can be broken down this way, by finding
all of the powers of 2 that add up to the number in question (in this case 26, 24, 22 and 21). You
can see this is exactly analagous to the decimal deconstruction of the number 125 that was done
earlier. Likewise we can make a similar set of observations:
• To multiply a number by 2 you can simply shift it to the left by one digit, and fill
in the rightmost digit with a 0. To divide a number by 2, simply shift the number
to the right by one digit.
• To see how many digits a number needs, you can simply take the logarithm (base
2) of the number, and add 1 to it. The integer part of the result is the number of
digits. For instance,
log2(86) + 1 = 7.426.
The integer part of that is 7, so 7 digits are needed.
• With n digits, 2n unique numbers (from 0 to 2n-1) can be represented. If n=8, 256
(=28) numbers can be represented 0-255.
Exercises:
Exercises:
Let's look at how this changes the value of some binary numbers
Binary Unsigned Signed
0010 0011 35 35
1010 0011 163 -93
1111 1111 255 -1
1000 0000 128 -128
If Bit 7 is not set (as in the first example) the representation of signed and unsigned numbers is
the same. However, when Bit 7 is set, the number is always negative. For this reason Bit 7 is
sometimes called the sign bit. Signed numbers are added in the same way as unsigned numbers,
the only difference is in the way they are interpreted. This is important for designers of
arithmetic circuitry because it means that numbers can be added by the same circuitry regardless
of whether or not they are signed.
To form a two's complement number that is negative you simply take the corresponding positive
number, invert all the bits, and add 1. The example below illustrated this by forming the number
negative 35 as a two's complement integer:
3510 = 0010 00112
invert -> 1101 11002
add 1 -> 1101 11012
So 1101 1101 is our two's complement representation of -35. We can check this by adding up the
contributions from the individual bits
1101 11012 = -128 + 64 + 0 + 16 + 8 + 4 + 0 + 1 = -35.
The same procedure (invert and add 1) is used to convert the negative number to its positive
equivalent. If we want to know what what number is represented by 1111 1101, we apply the
procedure again
? = 1111 11012
invert -> 0000 00102
add 1 -> 0000 00112
Since 0000 0011 represents the number 3, we know that 1111 1101 represents the number -3.
Exercises:
Answ er
Convert -120 from decimal to binary
Note that a number can be extended from 4 bits to 8 bits by simply repeating the leftmost bit 4
times. Consider the following examples
Decimal 4 bit 8 bit
3 0011 0000 0011
-3 1101 1111 1101
7 0111 0000 0111
-5 1011 1111 1011
Let's carefully consider the last case which uses the number -5. As a 4 bit number this is
represented as
1011 = -8 + 2 + 1 = -5
The 8 bit number is
1111 1011 = -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5.
It is clear that in the second case the sum of the contributions from the leftmost 5 bits (-128 + 64
+ 32 + 16 + 8 = -8) is the same as the contribution from the leftmost bit in the 4 bit
representation (-8)
This process is refered to as sign-extension, and can be applied whenever a number is to be
represented by a larger number of bits. In the 320C50 Digital Signal Processor, this typically
occurs when moving a number from a 16 bit register to a 32 bit register. Whether or not sign-
extension is applied during such a move is determined by the sign-extension mode bit. Note that
to store a 32 bit number in 16 bits you can simply truncate the upper 16 bits (as long as they are
all the same as the left-most bit in the resulting 16 bit number - i.e., the sign doesn't change).
Most processors even have two separate instructions for shifting numbers to the right (which,
you will recall, is equivalent to dividing the number in half). The first instruction is something
like LSR (Logical Shift Right) which simply shifts the bits to the right and usually fills a zero in
as the lefmost bit. The second instruction is something like ASR (Arithmetic Shift Right), which
shifts all of the bits to the right, while keeping the leftmost bit unchanged. With ASR 1010 (-6)
becomes 1101 (-3). Of course, there is only one instruction for a left shift (since LSL is
equivalent to ASL).
Positive binary fractions
The representation of unsigned binary fractions proceeds in exactly the same way as decimal
fractions. For example
0.62510 = 1*0.5 + 0*0.25 + 1*0.125 = 1* 2-1 + 0* 2-2 + 1* 2-3 = 0.1012
Each place to the right of the decimal point represents a negative power of 2, just as for decimals
they represent a negative power of 10. Likewise, if there are m bits to the right of a decimal, the
precision of the number is 2-m (versus 10-m for decimal). Though it is possible to represent
numbers greater than one by having digits to the left of the decimal place we will restrict
ourselves to numbers less than one. These are commonly used by Digital Signal Processors.
The largest number that can be represented by such a representation is 1-2-m , the smallest number
is 2-m. For a fraction with 15 bits of resolution this gives a range of approximately 0.99997 to
3.05E-5.
Note that this representationis easily extended to represent all positive numbers by having the
digits to the left of the decimal point represent the integer part, and the digits to the right
representing the fractional part. Thus
6.62510 = 110.1012
Exercises:
Answ er
Convert -0.65625 from decimal to a binary fraction
Signed binary fractions are easily extended to include all numbers by representing the number to
the left of the decimal point as a 2's complement integer, and the number to the right of the
decimal point as a positive fraction. Thus
-6.62510 = (-7+0.375)10 = 1001.0112
Note, that as with two's complement integers, the leftmost digit can be repeated any number of
times without affecting the value of the number.
A Quicker Method for Converting Binary Fractions.
Another way to convert Qm numbers to decimal is to represent the binary number as a signed
integer, and to divide by 2m. To convert a decimal number to Qm, multiply the number by 2m and
take the rightmost m digits. Note, this simply truncates the number; it is more elegant, and
accurate, but slightly more complicated, to round the number.
Examples (all Q7 numbers):
Bottom of Form