SLIDE 4 Integer Representations
An int is represented by a fixed-length sequence of bits A bit is a binary digit: its value is either 0 or 1. On typical architectures today, int’s are 32 bits How many different values can be represented by n bits?
1 bit: 2 values [0 and 1] 2 bits: 4 values [00, 01, 10, 11] 3 bits: 8 values [000, 001, 010, 011, 100, 101, 110, 111]
N bits: 2 choices for 1st bit, times 2 for 2nd bit choices, times 2 for 3rd bit
choices, etc … so n bits can represent 2n different values.
Thus there is a largest int value Largest = 231 – 1, which is about 2 billion How to deal with larger integer values? Use floats? What could be wrong with that?
Possibility of round-off error
Do what other languages do? (Answer: overflow)
Q2