10.3 Integer Arithmetic
COS2621 - Computer Organisation · 10. Computer Arithmetic
10.3 Integer Arithmetic
Introduction to Integer Arithmetic
Integer arithmetic involves operations on whole numbers, such as addition, subtraction, multiplication, and division. These operations are fundamental in computer systems, as they allow for calculations essential to various applications.
Addition of Integers
In binary, integers are represented using bits (0s and 1s). To perform addition, align the numbers and add them bit by bit, carrying over when the sum exceeds 1. For example, to add the binary numbers 1100 (12 in decimal) and 1011 (11 in decimal):
1100 (12 in decimal)
+ 1011 (11 in decimal)
----------------------
10111 (23 in decimal)The result is 10111, which is 23 in decimal. A carry occurs when both bits are 1, resulting in a 0 in that column and carrying 1 to the next column.
Watch out: Ensure you correctly handle carries in addition. Missing a carry can lead to incorrect results.
Subtraction of Integers
Subtraction can be performed using the two's complement method. To subtract a number, add its two's complement. For instance, to subtract 7 (0111) from 15 (1111):
1111 (15 in decimal)
+ 1001 (two's complement of 0111)
----------------------
11000Discard the leftmost bit, resulting in 0111 (8 in decimal). The result is 1000, which is 8 in decimal.
Multiplication of Integers
Multiplication of binary integers is similar to decimal multiplication. You shift and add. For example, to multiply 6 (0110) by 3 (0011):
0110 (6 in decimal)
x 0011 (3 in decimal)
----------------------
0000 (6 * 0)
+ 0110 (6 * 1, shifted left)
+ 0110 (6 * 1, shifted left again)
----------------------
1110 (18 in decimal)The result is 1110, which is 18 in decimal.
Division of Integers
Division can be performed using repeated subtraction or bit-shifting. For example, to divide 30 (11110) by 5 (0101):
11110 (30 in decimal)
/ 0101 (5 in decimal)
----------------------
0110 (6 in decimal)The result is 0110, which is 6 in decimal.
Overflow in Integer Arithmetic
Overflow occurs when the result of an arithmetic operation exceeds the maximum value that can be represented with the given number of bits. For example, in an 8-bit system, adding 255 (11111111) and 1 results in 00000000, which is interpreted as 0 due to overflow.
Watch out: Be aware of overflow conditions, especially in signed arithmetic, as they can lead to unexpected results.
Summary
- Integer arithmetic includes addition, subtraction, multiplication, and division.
- Carry handling is crucial in addition.
- Two's complement is used for subtraction.
- Overflow can lead to incorrect results.
Check your understanding
- What is the result of adding 1101 and 1010 in binary?
- How do you perform subtraction using two's complement?
- Explain how overflow occurs in binary addition.
Test yourself
5 quick questions on this lesson. Pick an answer to see if you're right.
1. What is the two's complement of the binary number 0110?
2. ___ instructions provide computational capabilities for processing numeric data.
3. What happens when an overflow occurs in binary addition?
4. What is the result of adding the binary numbers 1010 and 1100?
5. Which method is used to perform subtraction in binary arithmetic?