# Representing Integers

Summary: This document introduces a variety of mechanisms for representing integers in binary (as a sequence of 0's and 1's).

## Basic Concepts Behind Binary Representation

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 2-hundreds plus 9-tens plus 3-ones.

H T O
2 9 3

Years later, we learned that the ones column meant 100 , the tens column meant 101, the hundreds column 102 and so on. Hence, we started to think about 293 as (2*102)+(9*101)+(3*100). In columnar form,

102 101 100
2 9 3

As you know, the decimal system uses the digits 0-9 to represent numbers. If we wanted to put a larger number in column 10n (e.g., 10), we would have to multiply 10*10n, which would give 10n+1, and be carried a column to the left. For example, putting ten in the 100 column is impossible, so we put a 1 in the 101 column, and a 0 in the 100 column, thus using two columns. Twelve would be 12*100, or 100 (10+2), or 101+2*100, 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

102 101 100

they are

22 21 20

Instead of using the digits 0-9, we only use 0-1 (again, if we used anything larger it would be like multiplying 2*2n and getting 2n+1, which would not fit in the 2n 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 right-most column, which is 20, or 1. Since 3>1, we need to use an extra column to the left, and indicate 3 as `11` in binary (1*21) + (1*20).

## Binary Operations

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 one-bit 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*21)+(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 multiple-bit binary numbers:

```         1010
+1111
______
```
1. Column 20: 0+1=1.
Record the 1.
Temporary Result: 1; Carry: 0
2. Column 21: 1+1=10.
Record the 0, carry the 1.
Temporary Result: 01; Carry: 1
3. Column 22: 1+0=1 Add 1 from carry: 1+1=10.
Record the 0, carry the 1.
Temporary Result: 001; Carry: 1
4. Column 23: 1+1=10. Add 1 from carry: 10+1=11.
Record the 11.
Final result: 11001

Pictorially,,

```     11   (carry)
1010
+ 1111
______
11001
```

Always remember

• 0+0=0
• 1+0=1
• 1+1=10

### Unsigned Binary Multiplication

Single-digit multiplication in the binary system works the same way as in the decimal system:

• 1*1=1
• 1*0=0
• 0*1=0

Multiple-digit 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.

### Unsigned Binary Division

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
```

## Representing Signed Integers

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 2m-1.

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 8-bit number is 8 digits long and a 16-bit number is 16 digits long. Although most languages represent integers with 32 or 64 bits, for this section, we will work with 8 bits.

### Signed Magnitude

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 left-most 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*23) + (1*22)). To indicate -12, we would simply put a 1 rather than a 0 as the first bit: 10001100.

In eight bit signed-magnitude representation, the largest possible number is 01111111, or 127, and the smallest possible numbers is 11111111, or -127.

It turns out that the signed-magnitude 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.

### One's Complement

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.

### Two's Complement

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.

### Excess 2m-1

In this notation, m indicates the total number of bits. If we're working with an 8-bit representation, we would use excess 27 notation. To represent a number (positive or negative) in excess 27, add 27 (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 27+22+21+20, and, in binary, 10000111. We would represent -7 as 128-7=121, and, in binary, 01111001.

The largest possible number in excess 27 representation is 1111111, or 127. The smallest possible number is -128, or 00000000. There is only one representation for 0, 10000000.

## Investigating Signed Representations

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:

• 163, in unsigned binary,
• -35, in signed-magnitude,
• -92, in one's complement,
• -93, in two's complement,
• 35, in excess 27

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 27

```    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 27. However, it's easy to fix the results for excess 27: simply flip the leftmost bit.

Excess 27

```    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 2m-1, most computers use one of the those two representations.

## History

Friday, 31 January 2003 [Samuel A. Rebelsky]

• Created.
• Much of the content was taken from The Binary System: A Pretty damn clear guide to a quite confusing concept by Christine R. Wright, with some help from Samuel A. Rebelsky. The only version of that document I can currently find is at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/1997F/Readings/student-binary.html`.
• Dropped a section on converting between representations.
• Dropped problems.
• Reformatted.
• Changed the phrases negative numbers and potentially-negative numbers to signed numbers.
• Lots and lots of other small changes.

Sunday, 2 February 2003 [Samuel A. Rebelsky]

• Corrected a few typos.
• Verified that HTML is valid.

Tuesday, 4 February 2003 [Samuel A. Rebelsky]

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

Samuel A. Rebelsky, rebelsky@grinnell.edu