10.2 Integer Representation

COS2621 - Computer Organisation · 10. Computer Arithmetic

10. Computer Arithmetic

10.2 Integer Representation

Integer representation is crucial in computer systems as it defines how numbers are stored and manipulated. Computers use binary, a base-2 numeral system, to represent integers. Each digit in binary is called a bit, and a group of bits can represent a wide range of integer values.

Binary Representation

In binary, the value of each bit represents a power of 2. For example, the binary number 1101 can be calculated as follows:

1 × 2^3 + 1 × 2^2 + 0 × 2^1 + 1 × 2^0 = 8 + 4 + 0 + 1 = 13.

Thus, the binary number 1101 represents the decimal number 13.

Fixed-Size Representation

Computers typically use fixed-size representations for integers. Common sizes include:

  • 8 bits (1 byte): Can represent values from 0 to 255 (unsigned) or -128 to 127 (signed).
  • 16 bits (2 bytes): Can represent values from 0 to 65,535 (unsigned) or -32,768 to 32,767 (signed).
  • 32 bits (4 bytes): Can represent values from 0 to 4,294,967,295 (unsigned) or -2,147,483,648 to 2,147,483,647 (signed).

Remember: An unsigned byte uses 8 bits for its numeric value.

Two's Complement Representation

To represent negative integers, computers use two's complement. The two's complement of a binary number is found by inverting the bits and adding one. For example, to find the two's complement of 6 (which is 00000110 in binary):

  1. Invert the bits: 11111001.
  2. Add 1: 11111001 + 1 = 11111010.

The result, 11111010, represents -6 in an 8-bit two's complement format.

The highest bit (leftmost) indicates the sign in two's complement: 0 for positive and 1 for negative.

Examples of Integer Representation

Consider the decimal number -4. To represent -4 in an 8-bit two's complement:

  1. 4 in binary is 00000100.
  2. Invert the bits: 11111011.
  3. Add 1: 11111011 + 1 = 11111100.

Thus, -4 is represented as 11111100 in 8-bit two's complement.

Watch out: Do not confuse signed and unsigned representations. Signed integers can represent negative values, while unsigned integers cannot.

Summary

  • Binary is the fundamental numeral system for integers in computers.
  • Fixed-size representations are common, with 8, 16, and 32 bits being typical sizes.
  • Two's complement is used for negative integers, allowing for simple arithmetic operations.

Check your understanding

  • What is the binary representation of the decimal number 15?
  • How do you find the two's complement of a binary number?
  • What range of values can be represented with a 16-bit signed integer?
  • Explain the difference between signed and unsigned integer representation.

Test yourself

5 quick questions on this lesson. Pick an answer to see if you're right.

  1. 1. What range of values can be represented with a 16-bit signed integer?

  2. 2. An unsigned byte uses _ bits for its numeric value.

  3. 3. How do you find the two's complement of a binary number?

  4. 4. What is the main difference between signed and unsigned integer representation?

  5. 5. What is the binary representation of the decimal number 13?