CS Behind the Curtain (CS195 2003S)
Primary:
[Front Door]
[Current]
[Glance]

[Blurb]
[Disabilities]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Walker/Fall 2001]
[SamR]
Summary: This document introduces a variety of mechanisms for representing integers in binary (as a sequence of 0's and 1's).
Internally, most computers represent most values as a sequence of 0's and 1's. The number system in which all numbers are sequences of 0's and 1's is called the binary system.
To understand binary numbers, begin by recalling elementary school math.
When we first learned about numbers, we were taught that, in the
decimal system, things are organized into columns, such that H
is the hundreds column, T
is the tens column, and O
is
the ones column. Hence, we know that number 293
is 2hundreds
plus 9tens plus 3ones.
H  T  O 

2  9  3 
Years later, we learned that the ones column meant 10^{0 }, the tens column meant 10^{1}, the hundreds column 10^{2} and so on. Hence, we started to think about 293 as (2*10^{2})+(9*10^{1})+(3*10^{0}). In columnar form,
10^{2}  10^{1}  10^{0} 

2  9  3 
As you know, the decimal system uses the digits 09 to represent numbers. If we wanted to put a larger number in column 10^{n} (e.g., 10), we would have to multiply 10*10^{n}, which would give 10^{n+1}, and be carried a column to the left. For example, putting ten in the 10^{0} column is impossible, so we put a 1 in the 10^{1} column, and a 0 in the 10^{0} column, thus using two columns. Twelve would be 12*10^{0}, or 10^{0 }(10+2), or 10^{1}+2*10^{0}, which also uses an additional column to the left (12).
This binary system works under the exact same principles as the decimal system, only it operates in base 2 rather than base 10. In other words, instead of columns being
10^{2}  10^{1}  10^{0} 

they are
2^{2}  2^{1}  2^{0} 

Instead of using the digits 09, we only use 01 (again, if we used
anything larger it would be like multiplying 2*2^{n} and getting
2^{n+1}, which would not fit in the 2^{n} column.
Therefore, it would shift you one column to the left. For example,
"3" cannot be put into one column in binary represnation. The first
column we fill is the rightmost column, which is 2^{0}, or 1.
Since 3>1, we need to use an extra column to the left, and indicate
3 as 11
in binary (1*2^{1}) + (1*2^{0}).
Consider the addition of decimal numbers:
23 + 48 ____
We begin by adding 3+8=11. Since 11 is greater than 10, a one is put into the 10's column (carried), and a 1 is recorded in the one's column of the sum. Next, add 2+4+1 (the one is from the carry)=7, which is put in the 10's column of the sum. Thus, the answer is 71.
Unsigned binary addition works on the same principle, but the numerals are different. We begin with onebit binary addition:
0 0 1 + 0 + 1 + 0 ___ ____ ___ 0 1 1
1+1 carries us into the next column. In decimal form, 1+1=2. In binary,
any digit higher than 1 puts us a column to the left (as would 10 in
decimal notation). The decimal number 2
is written in binary
notation as 10
(1*2^{1})+(0*2^{ 0}). Record the
0 in the ones column, and carry the 1 to the twos column to get an answer
of 10
. In our vertical notation,
1 + 1 ____ 10
The process is the same for multiplebit binary numbers:
1010 +1111 ______
Pictorially,,
11 (carry) 1010 + 1111 ______ 11001
Always remember
Singledigit multiplication in the binary system works the same way as in the decimal system:
Multipledigit binary multiplication can be performed just as multiple digit decimal multiplication.
101 * 11 _____ 101 101 _____ 1111
Note that multiplying by two is extremely easy. To multiply by two, just add a 0 on the end.
For binary division, we can also follow the same kinds of rules as in decimal division. For the sake of simplicity, we'll throw away any remainder. As an example, let's compute 111011/11
10011 r 10 _______ 11)111011 11 ______ 101 11 ______ 101 11 ______ 10
Our discussion so far has focused on unsigned integers, integers
which we always assume to be positive. However, in practice, we need both
positive and negative integers. Such integers are called signed
integers. In the decimal system, we use an extra symbol,

(the minus sign
) to indicate that a value is negative.
We use an optional extra symbol, +
(the plus sign
) to
indicate that a value is positive. In binary, all we have are 0's and 1's.
It turns out, there are many possible mechanisms for representing signed
numbers, include signed magnitude,
one's complement,
twos's complement,
and
excess 2^{m1}.
Before we investigate signed numbers, we note that the computer uses a
fixed number of bits
(short for binary digits), for most numbers.
For example, an 8bit number is 8 digits long and a 16bit number is 16
digits long. Although most languages represent integers with 32 or 64
bits, for this section, we will work with 8 bits.
The simplest way to indicate negation is signed magnitude.
The key idea in signed magnitude is to use the first bit as the sign,
just as the leftmost thing
is a sign (plus or minus) in signed
decimal numbers. In binary signed magnitude, the leftmost bit is not
actually part of the number, but is just the equivalent of a +/ sign.
0
indicates that the number is positive, "1" indicates negative.
In 8 bits, 00001100 would be 12 (break this down into
(1*2^{3}) + (1*2^{2})). To indicate 12, we would simply put a 1
rather than a 0
as the first bit: 10001100.
In eight bit signedmagnitude representation, the largest possible number is 01111111, or 127, and the smallest possible numbers is 11111111, or 127.
It turns out that the signedmagnitude representation is not appropriate for all applications. For example, there are two ways to represent the value 0: 00000000 and 10000000. There are many other drawbacks, some of which we discuss later. Because of these drawbacks, computer scientists have designed a number of other representations.
In one's complement notation, positive numbers are represented as usual in unsigned binary. However, negative numbers are represented differently. To negate a number, replace all zeros with ones, and ones with zeros: flip the bits. Thus, 12 would be 00001100, and 12 would be 11110011. As in signed magnitude, the leftmost bit indicates the sign (1 is negative, 0 is positive). To compute the value of a negative number, flip the bits and translate as before.
In eight bit one's complement representation, the largest possible number is 01111111, or 127, and the smallest possible numbers is 10000000, or 127.
Once again, we have the drawback of having two different representations for zero. However, there are some advantages of the one's complement representation, which we discuss later.
The two's complement representation is a slight variation of one's complement, with a twist. To represent a number in two's complement, begin with the number in one's complement. If the number is negative, add 1 using unsigned binary addition and throw away the leftmost bit if the number ends up using nine bits.
In two's complement notation, twelve would be represented as 00001100, and negative twelve as 11110100. To verify this, let's subtract 1 from 11110110, to get 11110011. If we flip the bits, we get 00001100, or 12 in decimal.
This notation does not seem very natural to many people, but it does have the advantage that negative 0 is 0. Start with 00000000. Flip all the bits: 11111111. Add 1: 100000000. Throw away the leftmost bit: 00000000.
Because of this single representation, we can represent one more number. The largest number is still 01111111, or 127. The smallest is 10000000, or 128.
In this notation, m
indicates the total number of bits.
If we're working with an 8bit representation, we would use excess
2^{7} notation. To represent a number (positive or negative)
in excess 2^{7}, add 2^{7} (or 128) to that number and
then represent the result in unsigned binary notation.
For example, to represent 7 would be 128 + 7=135, or 2^{7}+2^{2}+2^{1}+2^{0}, and, in binary, 10000111. We would represent 7 as 1287=121, and, in binary, 01111001.
The largest possible number in excess 2^{7} representation is 1111111, or 127. The smallest possible number is 128, or 00000000. There is only one representation for 0, 10000000.
Because computer scientists have developed these alternative representations, in order to interpret a sequence of bits, you need to know which representation has been used. The sequence 10100011 can be:
How do we select a representation to use? To see the advantages and disadvantages of each method, you might try working with them. Here's a way to start: Using the regular algorithm for binary adition, add (5+12), (5+12), (12+5), and (12+12) in each system. Then, convert back to decimal numbers.
Signed Magnitude:
5+12 5+12 12+5 12+12 00000101 10000101 10001100 00001100 + 00001100 + 00001100 + 10000101 + 10001100 __________ __________ __________ ___________ 00010001 10010001 00010000 10011000 17 17 16 24
One's Complement:
5+12 5+12 12+5 12+12 00000101 11111010 11110011 00001100 + 00001100 + 00001100 + 11111010 + 11110011 ___________ __________ __________ __________ 00010001 00000110 11101101 11111111 17 6 18 0
Two's Complement:
5+12 5+12 12+5 12+12 00000101 11111011 11110100 00001100 + 00001100 + 00001100 + 11111011 + 11110100 __________ __________ __________ __________ 00010001 00000111 11101111 00000000 17 7 17 0
Excess 2^{7}
5+12 5+12 12+5 12+12 10000101 01111011 01110100 10001100 + 10001100 + 10001100 + 01111011 + 01110100 __________ __________ __________ __________ 00010001 00000111 11101111 00000000 111 121 111 128
As you can see, this technique seems to work best for two's complement, and works very strangely for excess 2^{7}. However, it's easy to fix the results for excess 2^{7}: simply flip the leftmost bit.
Excess 2^{7}
5+12 5+12 12+5 12+12 10000101 01111011 01110100 10001100 + 10001100 + 10001100 + 01111011 + 01110100 __________ __________ __________ __________ 00010001 00000111 11101111 00000000 Flip 10010001 10000111 01101111 10000000 17 7 17 0
Because addition is easiest for two's complement and excess 2^{m1}, most computers use one of the those two representations.
Friday, 31 January 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/1997F/Readings/studentbinary.html
.
negative numbersand
potentiallynegative numbersto
signed numbers.
Sunday, 2 February 2003 [Samuel A. Rebelsky]
Tuesday, 4 February 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Readings/integers.html
.
Primary:
[Front Door]
[Current]
[Glance]

[Blurb]
[Disabilities]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Walker/Fall 2001]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Fri May 2 14:21:25 2003.
The source to the document was last modified on Tue Feb 4 13:42:29 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Readings/integers.html
.
; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu