Image goes here
Integers
CptS 260 - Intro to Computer Architecture
Washington State University

Representing numbers

This lecture: integers representations; next time: arithmetic

Binary representation

Why binary? Digital electric circuits work best as on/off. Seek representations that work well with this fact.

Expansion of binary number as multiples of powers of two. Binary is painful because it takes up so much space when written. Octal and hex have become conventional shorthands that allow easy interconversion with binary.

Octal and Hex Representations

Mention octal: 8 digits, 0-7; each octal digit represents 3 bits; inconvenient for 16 and 32-bit words
Conversion of binary to hex: groups of 4 from R to L; each 4 bits has a direct correspondence to a hex digit, 0-F. 4-bits is called a nibble (half of a byte -- 8 bits). Who said computer scientists don't have a sense of humor?
Encourage familiarity with approximate values of
  • 0x400 = 210 = 1024 = approx. 103, kilo vs. kibi
  • 0x1000 = 212 = 4096,
  • 0x8000 = 215 = 32768,
  • 0x10000 = 216= 65536,
  • 0x100000 = 220 = approx. 10^6, mega vs. mibi
  • 0x40000000 = 230 = approx. 10^9, giga vs. gibi
  • 0x80000000 = 231 = approx. 2 billion.
  • 0x100000000 = 232 = approx. 4 billion,

Typically, computer arithmetic takes place with fixed word sizes: 8, 16 and 32 bits; this gives modular arithmetic. If we add two numbers of sufficiently large size we get a smaller (!) number. This is an example of overflow.

Different computers use different word sizes: how many bits (maximum) does the processor access at one time. Affects: size of registers, size of buses, maximum addressable memory (often somewhat decoupled using various architectural tricks).

The word size determines the maximum value of integer that the processor can handle in one instruction. Multi-word representations are possible but arithmetic then involves multiple instructions.

Representing negative numbers

  • sign and magnitude -- issues
  • invert all the bits -- one's complement
  • two's complement:
    • definition: -n is represented by 2b-n where b is the wordsize. Note that this is true for both positive and negative n, where |n| < 2(b-1). -(2(b-1)) is representable but 2(b-1) is not. In total 2b different values are representable.
    • Simplified computation of two's complement value: subtract from all-1's value (of length b) and add 1 == invert each bit and add 1.
    • Another simplified computation: subtract 1 from the value and invert all the bits.
    • The value of a two-s complement number is given by
      -db-12b-1+db-22b-2+...+d121+d020

    Ten's complement arithmetic: subtract without borrowing.

    Two's complement may seem mysterious, but the same approach can be taken to decimal arithmetic. To form a negative, subtract each digit from 9. Add 1 to the result. Remember, you have to be consistent about how many digits you are using. This has the wonderful property that you never have to borrow when doing subtraction:
          12345-06789=?
          12345+93211=    105556
    
    Furthermore, when adding a positive number and a negative number you don't have to worry about which is larger.

Computing with 2's-complement numbers

  • computing with two's complement numbers: add, subtract, multiply: treat just like unsigned numbers -- choose to interpret as two's complement.
  • Basic fact: adding a positive and negative number can never overflow
  • Adding two positive or two negative can overflow. How is this reflected in the answer? Example:
          0110 1101
          0101 1000
    =     1100 0101
    
    Addition (or subtraction) of two n-bit numbers may produce at most an n+1-bit result. How handled? Result is (a+b) mod 2n in the unsigned case. A separate status register may have a bit set to indicate that a carry occurred. Typically this bit can be tested with a branch instruction so software can detected the overflow. Some processors can associate an interrupt with overflow.
  • We can also choose to interpret computer words as unsigned numbers. So: 0xFFFFFFFF can be interpreted as -1 (twos complement) or 2^32-1 (unsigned). Programmers have to be careful when mixing interpretations: the HW does not help!

    Have to be careful at just a couple of points: loading a shorter value into a longer word -- do sign extension or not? And when shifting right -- shift the leading bit or not. Look at shifts (explain difference between arithmetic and logical shifts). Sign extension; note that a negative number always has the high-order bit set (suggest "prove it").

Multiplication: n-bit multiplication can produce a 2n bit result. An n-bit processor may provide the upper (most-significant) bits in a separate register from the lower n bits.

Interpretation of binary data

Returning to interpretation:

Binary data can also be treated as characters.

  • 5-bit codes ITA-2 teletype code
  • 7-bit codes ASCII (circa-1965)
  • 8-bit codes EBCDIC (also circa-1965), various extended ASCII codes including ISO 8859-1, etc.
  • multi/variable byte codes Unicode circa (1990)
Might also interpret as a machine instruction or a pointer, or a floating point number.

Given 8 bits is it a character? a number? part of a number? part of a character?
You can't tell! It is entirely up to software to determine the context in which it is used and do the right thing with it.

In the past, some computers tagged memory locations with the type of the value that they contained. Modern processors leave interpretation of values to software. Why? improved languages that track types better, allowing fewer type errors and permitting automatic choice of the correct operations. Also, tags take up valuable memory space.

Exercises

  1. Convert 783 (base 10) to 16-bit binary and hex and octal
  2. Convert -783 (base 10) to twos-complement binary and hex and octal (16-bit)
  3. Add -783 and -75 using 16-bit two's complement arithmetic. Check your answer using 3-digit 10's complement arithmetic.
  4. Multiply -783 and -23 using 16-bit two's complement arithmetic. Check your answer using normal base 10 arithmetic. What happened? Now do the computation using 32-bit two's complement arithmetic. If you use 10's complement arithmetic how many digits do you have to use to represent the multiplicands in order to get a correct answer?
(c) 2004-2006 Carl H. Hauser           E-mail questions or comments to Prof. Carl Hauser