Lecture 2: Ints Flashcards
(12 cards)
What are the objectives of this lecture? (3)
(1) working with and around resource limitations, (2) employing integer algorithms - binary addition, (3) identifying , describing, and effectively using integers as signed modular arithmetic and as fixed length bit vectors in c0
Easy method to convert from binary to decimal
1 = 1 1 * 2 + 0 = 2 2* 2 + 0 = 4 4 * 2 + 1 = 9 9 * 2 + 0 = 18 18 * 2 + 1 = 37 37 * 2 + 1 = 75 75 * 2 + 0 = 150
Why is overloading not a good idea for dealing with limited storage space?
(1) somewhat expensive because overflow condition must be explicitly detected, (2) the statement (n+n)-n = n+(n-n) would no longer be equal, as the first might give you an overload error while the second would give you n
Why is modular arithmetic a good solution to dealing with limited storage space for ints?
Many of the usual laws of arithmetic continue to hold. Table of properties of the abstract algebra class of rings shared between ordinary integers and integers modulo a fixed number n List of properties: (1) commutativity of add (2) associativity of add (3) additive identity/unit (4) additive inverse (5) cancellation: -(-x)=x (6) commutativity of mult (7) associativity of mult (8) multiplicative identity/unit (9) distributivity (10) annihilation: x * 0 = 0
How do we define negative numbers?
as additive inverses: -x is the number y s.t. x + y = 0
How does modular arithmetic lend itself to additive inverses?
The mod of a number x, is the additive inverse of -x.
x = y mod(16) implies x+y=0
How can we write out the equivalence classes of numbers modulo 16 together with binary representation?
(-16, 0, 16, 0000) (-15, 1, 17, 0001) (-14, 2, 18, 0010) (-13, 3, 19, 0011) (-12, 4, 20, 0100) (-11, 5, 21, 0101) (-10, 6, 22, 0110) (-9, 7, 23, 0111) (-8, 8, 24, 1000) (-7, 9, 25, 1001) (-6, 10, 26, 1010) (-5, 11, 27, 1011) (-4, 12, 28, 1100) (-3, 13, 29, 1101) (-2, 14, 30, 1110) (-1, 15, 31, 1111)
How do you figure out the decimal value of a binary number that has a leading one? (ex. 1101, and 1001)
Get the mod number (2^4=9).
For 1101: 8+2+1=11 and 11 mod 16 is equal to 11 and -5. We use -5 because we want an even number of positive and negative numbers
What is the binary procedure to convert from x to -x? How did we derive this procedure?
fir complement all the bits, then add 1; derived the procedure from y= -x and x + y = 10000 (base 2) = 0 (mod 16).
If we said -x was just the complement of x, then x + -x = 1111 (base 2) mod 16 = 15 mod (16) = -1 so we’re still one short
What is the maximum number in 4-bit numbers? What is the minimum?
maximum: 0111 = 7; minimum: 1000 = 8, and 8 mod 16 = -8
What is the generalized formula for the ranges of positive and negative numbers in a representation with p bits?
positives range from 0 to 2^p-1 and the negatives range from -2^p to -1
What is the expression that relates integer division and integer modulus?
[need to be continued]