# University of Calgary - Computer Science

## Mathematical Operations With Signed Numbers

Signed Binary Numbers

So far, we've treated all binary numbers in this discussion as unsigned numeric values. However, we can also use signed binary numbers.

What makes one number signed, and another number unsigned? Whether a number is a signed number or an unsigned number depends solely on how we treat the number in our program. If we perform mathematics and check the SIGN Flag in the Flag Register or test the MSB for a 0 or 1, we are treating the value as a signed number.

The binary number 0000 0000 represents zero, 0000 0001 represents one, 0000 0010 represents two, and so on toward the largest number that we care to represent.

What about negative numbers? How can we place a minus sign in a binary position? Well, we can't. So we assign a bit, the MSB, as a SIGN bit.

The rule for signed and unsigned binary numbers is simple:

1. In an Unsigned Number, the MSB is a weighted position bit.
2. In a Signed Number, the MSB (the sign bit) is 0 for a Positive number.
3. In a Signed Number, the MSB (the sign bit) is 1 for a Negative number.

The number of numeric values that can be represented by any

• number is 2^n where n is the number of bits.
• the range of values for unsigned numbers is 0 through (2^n) - 1, the number of different numeric values minus 1
• the range of values for a signed number is -2^(n-1) through +2^(n - 1) - 1

The table below contains the range of values for signed and unsigned binary numbers organized on a Byte, 16-bit Word, and 32-bit Double Word boundary.

 Size Byte Word Double Word Number of Values 256 65,536 4,294,967,296 Unsigned Number 0 to 255 0 to 65,535 0 to 4,294,967,295 Signed Number -128 to 127 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647

For the Byte, we represent the negative values in the range -128 through -1 and the positive values in the range 0 through 127 with a single eight bit byte.

With a 16-bit word we can represent negative values in the range -32,768 through -1, and positive values in the range 0 through +32,767.

With a 32-bit double word we can represent negative values in the range -2,147,483,648 through -1 and the positive values in the range 0 through +2,147,483,647.

Negative Binary Numbers - The 2's Complement

We do not just place a 1 in the MSB of a binary number to make it negative. We must take the 2's Complement of the number. Taking the 2's Complement of the number will cause the MSB to become 1.

To obtain the 2's complement of a number is a two step process.

1. Take the 1's complement of the number by changing every logic 1 bit in the number to a 0, and change every logic 0 bit to a 1.
2. Add 1 to the 1's Complement of the binary number. You now have the 2's Complement of the original number. You will notice that the MSB has become a 1.

The complement of a binary number, also known as the 1's complement, requires us to change every logic 1 bit in a number to a logic 0, and every logic 0 bit to a logic 1. Let's find the 1's complement of 36H or 0011 0110 in binary. In the following table, the 1's Complement is shown in RED.

 Number Format D7 D6 D5 D4 D3 D2 D1 D0 Unsigned Number 0 0 1 1 0 1 1 0 1's Complement 1 1 0 0 1 0 0 1

To obtain the 2's complement of a binary number, we must first obtain the 1's complement of the number and then add 1. In the following table, the 1's complement is RED and the 2's Complement is in BLUE. The 2's complement of 36H or 0011 0110 in binary is:

 Number Format D7 D6 D5 D4 D3 D2 D1 D0 Unsigned Number 0 0 1 1 0 1 1 0 1's Complement 1 1 0 0 1 0 0 1 2's Complement 1 1 0 0 1 0 1 0

If we are using signed binary numbers and the MSB is already logic 1, it means the value is the 2's complement of the number. The actual numeric value can be determined by taking the 2's complement of the number and keeping track of the minus sign mentally.

Find the decimal value of the signed binary number 0FFH.

 Number Format D7 D6 D5 D4 D3 D2 D1 D0 Signed Number 1 1 1 1 1 1 1 1 1's Complement 0 0 0 0 0 0 0 0 2's Complement 0 0 0 0 0 0 0 1

Therefore, 0FF is a signed number that has the value in decimal of -1.

Subtraction of Binary Numbers - Using 2's Complement Addition

If we add the 2's Complement of a signed number to another signed number, we are performing the mathematical operation of subtraction. This is, in fact. how many 8-bit microprocessors actually perform subtraction, they perform 2's complement addition.

Let us see if 0FFH is really -1. If we add it to 01H, we expect the result to be 0. One special condition about twos complement addition, overflows (a CARRY OUT which SETS the CARRY FLAG) from the register are ignored. So let's see what happens.

 In Decimal CO D7 D6 D5 D4 D3 D2 D1 D0 1 X 0 0 0 0 0 0 0 1 -1 X 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0

The last row of the table shows that the result is 0. The 1 in the CO (CARRY OUT) is ignored. If we ignore the CO, the result is 0000 0000.

Okay, so it works for a byte. How about a Word? It is the same procedure. Only the MSB is used as the sign bit, so the MSB of the low order byte is a weighted position bit. For 16-bit numbers:

• 8000H is negative because the high order bit is one.
• 0100H is positive because the high order bit is zero.
• 7FFFH is positive.
• 0FFFFH is negative.
• 0FFFH is positive.