You are on page 1of 18

Signed number representations

From Wikipedia, the free encyclopedia


Jump to: navigation, search
In computing, signed number representations are required to encode negative numbers in
binary number systems.
In mathematics, negative numbers in any base are represented by prefixing them with a − sign.
However, in computer hardware, numbers are represented in binary only without extra symbols,
requiring a method of encoding the minus sign. The four best-known methods of extending the
binary numeral system to represent signed numbers are: sign-and-magnitude, ones' complement,
two's complement, and excess-N. Some of the alternative methods use implicit instead of explicit
signs, such as negative binary, using the base -2. Corresponding methods can be devised for
other bases, whether positive, negative, fractional, or other elaborations on such themes. In
practice the representation most generally used in current computing devices is twos'
complement, although there is no definitive criterion by which any of the representations is
universally superior.

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] Sign-and-magnitude method


8 bit signed magnitude
Binary Signed Unsigned
00000000 +0 0
00000001 1 1
... ... ...
01111111 127 127
10000000 −0 128
10000001 −1 129
... ... ...
11111111 −127 255
One may first approach the problem of representing a number's sign by allocating one sign bit to
represent the sign: set that bit (often the most significant bit) to 0 for a positive number, and set
to 1 for a negative number. The remaining bits in the number indicate the magnitude (or absolute
value). Hence in a byte with only 7 bits (apart from the sign bit), the magnitude can range from
0000000 (0) to 1111111 (127). Thus you can represent numbers from −12710 to +12710 once you
add the sign bit (the eight bit). A consequence of this representation is that there are two ways to
represent zero, 00000000 (0) and 10000000 (−0). Decimal −43 encoded in an eight-bit byte this
way is 10101011.
This approach is directly comparable to the common way of showing a sign (placing a "+" or "−"
next to the number's magnitude). Some early binary computers (e.g. IBM 7090) used this
representation, perhaps because of its natural relation to common usage. Sign-and-magnitude is
the most common way of representing the significand in floating point values.
[edit] Ones' complement
8 bit ones' complement
Binary Ones' complement Unsigned
value interpretation interpretation
00000000 +0 0
00000001 1 1
... ... ...
01111101 125 125
01111110 126 126
01111111 127 127
10000000 −127 128
10000001 −126 129
10000010 −125 130
... ... ...
11111101 −2 253
11111110 −1 254
11111111 −0 255
Alternatively, a system known as ones' complement can be used to represent negative numbers.
The ones' complement form of a negative binary number is the bitwise NOT applied to it — the
"complement" of its positive counterpart. Like sign-and-magnitude representation, ones'
complement has two representations of 0: 00000000 (+0) and 11111111 (−0).
As an example, the ones' complement form of 00101011 (43) becomes 11010100 (−43). The
range of signed numbers using ones' complement is represented by −(2N−1−1) to (2N−1−1) and +/
−0. A conventional eight-bit byte is −12710 to +12710 with zero being either 00000000 (+0) or
11111111 (−0).
To add two numbers represented in this system, one does a conventional binary addition, but it is
then necessary to add any resulting carry back into the resulting sum. To see why this is
necessary, consider the following example showing the case of the addition of −1 (11111110) to
+2 (00000010).
binary decimal
11111110 -1
+ 00000010 +2
............ ...
1 00000000 0 <-- not the correct answer
1 +1 <-- add carry
............ ...
00000001 1 <-- correct answer
In the previous example, the binary addition alone gives 00000000, which is incorrect. Only
when the carry is added back in does the correct result (00000001) appear.
This numeric representation system was common in older computers; the PDP-1, CDC 160A and
UNIVAC 1100/2200 series, among many others, used ones'-complement arithmetic.
A remark on orthography: The system is referred to as "ones' complement" because the negation
of a positive value x (represented as the bitwise NOT of x) can also be formed by subtracting x
from the ones' complement representation of zero that is a long sequence of ones (-0). Two's
complement arithmetic, on the other hand, forms the negation of x by subtracting x from a single
large power of two that is congruent to +0.[1] Therefore, ones' complement and two's complement
representations of the same negative value will differ by one.
The Internet protocols IPv4, ICMP, UDP and TCP all use the same 16-bit ones' complement
checksum algorithm. Although most computers lack "end-around carry" hardware, the extra
complexity is accepted because "it is equally sensitive to errors in all bit positions".[2] In UDP,
the all 0s representation of zero indicates that the optional checksum feature has been omitted.
The other representation, FFFF, indicates a checksum value of 0.[3] (Checksums are mandatory in
IPv4, TCP and ICMP; they were omitted from IPv6).
Note that the ones' complement representation of a negative number can be obtained from the
sign-magnitude representation merely by bitwise complementing the magnitude.
[edit] Two's complement
8 bit two's complement
Two's
Binary Unsigned
complement
value interpretation
interpretation
00000000 0 0
00000001 1 1
... ... ...
01111110 126 126
01111111 127 127
10000000 −128 128
10000001 −127 129
10000010 −126 130
... ... ...
11111110 −2 254
11111111 −1 255
Main article: Two's complement
The problems of multiple representations of 0 and the need for the end-around carry are
circumvented by a system called two's complement. In two's complement, negative numbers are
represented by the bit pattern which is one greater (in an unsigned sense) than the ones'
complement of the positive value.
In two's-complement, there is only one zero (00000000). Negating a number (whether negative
or positive) is done by inverting all the bits and then adding 1 to that result. Addition of a pair of
two's-complement integers is the same as addition of a pair of unsigned numbers (except for
detection of overflow, if that is done). For instance, a two's-complement addition of 127 and
−128 gives the same binary bit pattern as an unsigned addition of 127 and 128, as can be seen
from the above table.
An easier method to get the negation of a number in two's complement is as follows:
Example 1 Example 2
1. Starting from the right, find the first '1' 0101001 0101100
2. Invert all of the bits to the left of that one 1010111 1010100

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

Representing fractions is a simple extension of this idea. To wit,


25.43 10 = 2*10 + 5*1 + 4*0.1 + 3*0.01 = 2*101 + 5*100 + 4*10-1+ 3*10-2
The only pertinent observations here are:
• If there are m digits to the right of the decimal point, the smallest number that can
be represented is 10-m. For instance if m=4, the smallest number that can be
represented is 0.0001=10-4.
After reading this dcoument you might want to learn something about binary arithmetic.
Binary Representation of positive integers
Binary representations of positive can be understood in the same way as their decimal
counterparts. For example
8610 = 1*64 + 0*32 + 1*16 + 0*8 + 1*4 + 1*2 + 0*1
or
8610 = 1* 2 + 0* 2 + 1* 2 + 0* 23 + 1* 22 + 1* 21 + 0* 20
6 5 4

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:

Convert 125 from decimal to binary

Convert 96 from decimal to binary

Convert 10011 from binary to decimal


In 'C', an unsigned integer is usually 16 bits. What
is the largest number that can be represented by an
unsigned integer?
Convert 37 to binary, shift it left by one and
convert back to decimal. What is the result

Hexadecimal, Octal, Bits, Bytes and Words.


It is often convenient to handle groups of bits, rather than individually. The most common
grouping is 8 bits, which forms a byte. A single byte can represent 256 (28) numbers. Memory
capacity is usually referred to in bytes. Two bytes is usually called a word, or short word (though
word-length depends on the application). A two-byte word is also the size that is usually used to
represent integers in programming languages. A long word is usually twice as long as a word. A
less common unit is the nibble which is 4 bits, or half of a byte.
It is cumbersome for humans to deal with writing, reading and remembering individual bits,
because it takes many of them to represent even fairly small numbers. A number of different
ways have been developed to make the handling of binary data easier for us. The most common
is hexadecimal. In hexadecimal notation, 4 bits (a nibble) are represented by a single digit. There
is obviously a problem with this since 4 bits gives 16 possible combinations, and there are only
10 unique decimal digits, 0 to 9. This is solved by using the first 6 letters (A..F) of the alphabet
as numbers. The table shows the relationship between decimal, hexadecimal and binary.
Decimal Hexadecimal Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111
There are some significant advantages to using hexadecimal when dealing with electronic
representations of numbers (if people had 16 fingers, we wouldn't be saddled with the awkward
decimal system). Using hexadecimal makes it very easy to convert back and forth from binary
because each hexadecimal digit corresponds to exactly 4 bits (log 2(16) = 4) and each byte is two
hexadecimal digit. In contrast, a decimal digit corresponds to log2(10) = 3.322 bits and a byte is
2.408 decimal digits. Clearly hexadecimal is better suited to the task of representing binary
numbers than is decimal.
As an example, the number CA3 16 = 1100 1010 00112 (11002 = C16 , 10102 = A16, 00112 = 3 16). It
is convenient to write the binary number with spaces after every fourth bit to make it easier to
read. Converting back and forth to decimal is more difficult, but can be done in the same way as
before.
323510 = C16*256 + A16*16 + 316*1 = C16 *162 + A16 *161 + 316 *160
or
323510 = 12*256 + 10*16 + 3*1 = 12*162 +10*161 +3*160
Octal notation is yet another compact method for writing binary numbers. There are 8 octal
characters, 0...7. Obviously this can be represented by exactly 3 bits. Two octal digits can
represent numbers up to 64, and three octal digits up to 512. A byte requires 2.667 octal digits.
Octal used to be quiete common, it was the primary way of doing low level I/O on some old
DEC computers. It is much less common today but is still used occasionally (e.g., to set read,
write and execute permissions on Unix systems)
In summary:
bit
a single binary digit, either zero or one.
byte
8 bits, can represent positive numbers from 0 to 255.
hexadecimal
A representation of 4 bits by a single digit 0..9,A..F. In this way a byte can be represented
by two hexadecimal digits
long word
A long word is usually twice as long as a word.
nibble
4 bits, half of a byte.
octal
A representation of 3 bits by a single digit 0..7. This is used much less commonly than it
once was (early DEC computers used octal for much of their I/O)
word
Usually 16 bits, or two bytes. But a word can be almost any size, depending on the
application being considered -- 32 and 64 bits are common sizes

Exercises:

Convert 2000 from decimal to hexadecimal

Convert 3C from hexadecimal to decimal

Convert 1010 0111 1011 from binary to


hexadecimal

Convert 7D0 from hexadecimal to binary

If you shift a hexadecimal number to the left by one


digit, how many times larger is the resulting
number?
Signed Binary Integers
It was noted previously that we will not be using a minus sign (-) to represent negative numbers.
We would like to represent our binary numbers with only two symbols, 0 and 1. There are a few
ways to represent negative binary numbers. The simplest of these methods is called ones
complement, where the sign of a binary number is changed by simply toggling each bit (0's
become 1's and vice-versa). This has some difficulties, among them the fact that zero can be
represented in two different ways (for an eight bit number these would be 0000 0000 and 1111
1111)., we will use a method called two's complement notation which avoids the pitfalls of one's
complement, but which is a bit more complicated.
To represent an n bit signed binary number the leftmost bit, has a special significance. The
difference between a signed and an unsigned number is given in the table below for an 8 bit
number.
The value of bits in signed and unsigned binary numbers
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Unsigned 2 = 128
7
2 = 64
6
2 = 32
5
2 = 16
4
2=8
3
2 =4
2
21 = 2 20 = 1
Signed -(27) = -128 26 = 64 25 = 32 24 = 16 23 = 8 22 = 4 21= 2 20 = 1

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:

Convert 1101 1101 from binary to decimal

Convert 0010 0010 from binary to decimal

Answ er
Convert -120 from decimal to binary

In 'C', a signed integer is usually 16 bits. What is


the largest positive number that can be
represented?
What is the largest negative number that can be
represented?

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:

Convert 0.100 1001 from binary to decimal

Convert 0.111 1111 from binary to decimal

Convert 0.75 from decimal to a binary fraction

Convert 0.65625 from decimal to a binary fraction

Approximate 0.9 as a binary fraction (use 8 bits)

Signed binary fractions


Signed binary fractions are formed much like signed integers. We will work with a single digit to
the left of the decimal point, and this will represent the number -1 (= -(20)). The rest of the
representation of the fraction remains unchanged. Therefore this leftmost bit represents a sign bit
just as with two's complement integers. If this bit is set, the number is negative, otherwise the
number is positive. The largest positive number that can be represented is still 1-2-m but the
largest negative number is -1. The resolution is still 1-2-m.
There is a terminology for naming the resolution of signed fractions. If there are m bits to the
right of the decimal point, the number is said to be in Qm format. For a 16 bit number (15 bits to
the right of the decimal point) this results in Q15 notation.
Exercises:

Convert 1.100 1001 from binary to decimal


Convert 1.111 1111 from binary to decimal

Convert -0.75 from decimal to a binary fraction

Answ er
Convert -0.65625 from decimal to a binary fraction

Approximate -0.9 as a binary fraction (use 8 bits)

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):

Take the binary number 0100 1001 (=7310), and divide by


Convert 0.100 27=128. The answer is 73/128=0.5703125, which agrees
1001 to decimal. with the result of the previous exercise (Positive Binary
Fractions).

Take the two's complements binary number 1100 1001 (=-


Convert 1.100 5510), and divide by 128. The answer is -0.4296875, which
1001 to decimal. agrees with the result of the previous exercise (Signed
Binary Fractions).

Multiply 0.9 by 128 to get 115.2. This is represented in


Convert 0.9 to binary as 111 0011, so the Q7 representation is 0.111
Q7 format 0011. This agrees with the result of the previous exercise
(Positive Binary Fractions).
Multiply -0.9 by 128 to get -115.2. The Q7 representation
Convert -0.9 to
is 1.000 1101. This agrees with the result of the previous
Q7 format
exercise (Signed Binary Fractions).

Bottom of Form