Skip to content

Integer Representations

  • This section describe 2 different ways bits can be used to encode integers:
    • Unsigned encodings: present non-negative numbers.
    • Two’s-complements encoding: present negative, zero and positive numbers.

Unsigned encodings:

B2U4([0001]) = 0 * 23 + 0 * 22 + 0 * 21 + 1 * 20 = 0 + 0 + 0 + 1 = 1

Two’s-complements encodings:

  • Use the most significant bit is called the sign bit.

    • If sign bit = 1, the represented value is negative
    • If sign bit = 0, the represented value is non-negative
  • Examples:

    • B2T4([0001]) = - 0 * 23 + 0 * 22 + 0 * 21 + 1 * 20 = 0 + 0 + 0 + 1 = 1
    • B2T4([0101]) = - 0 * 23 + 0 * 22 + 0 * 21 + 1 * 20 = 0 + 4 + 0 + 1 = 5
    • B2T4([1011]) = - 1 * 23 + 0 * 22 + 0 * 21 + 1 * 20 = -8 + 0 + 2 + 1 = -5
    • B2T4([1111]) = - 1 * 23 + 0 * 22 + 0 * 21 + 1 * 20 = -8 + 4 + 2 + 1 = -1

Expanding bits

Convert an unsigned number to a larger unsigned number:

  • Add leading zeros the the representation (zero-extension)

Convert a signed number to a larger signed number:

  • Add copies of the MSB to the representation (sign-extension)

Convert a signed number to a larger unsigned number (or reverse)

  • First changes the sign

  • Changes the type

  • Example:

    short sx = -12345; // 2's complement: 1100 1111 1100 1111 (CFCF)
    unsigned uy = sx; // equivalent to (unsigned) (int) sx;
    • Convert to a larger type: 1100 1111 1100 1111 => 1111 1111 1111 1111 1100 1111 1100 1111 (sign extension)
    • Change the type: simply convert the binary to decimal => result = 4294954951