Integer division always rounds it’s result by rounding towards 0. So if we do 5 the result is 2.5 which is rounded to 2. This is the same result that we get if we shift 0101 to the right one position, i.e., 0010. If we do −5, however, the 3 result is −2.5 which should round toward 0, i.e., to −2. The 2’s complement version of −5 however is 1011. If we shift it right we get 1101 or −3 not −2. To fix this, we can add one before we shift, i.e., −5 + 1 = −4 or 1100. When we shift this right we get 1110 or −2, the correct answer. How can we do this without an if statement to test whether the number is positive or negative? If a 2’s complement number is positive, the sign bit is 0. If the 2’s complement number is negative it’s sign bit is 1. So we shift the sign bit all the way to bit position 0 (63 bits if we have a 64 bit number). If the number is negative, it results in the value ”1”; if the number if positive it results in the value ”0”. We then add this to the number that we are to divide, i.e., we add ”1” to a negative number and ”0” to a positive number. Then when we shift right, the number will be rounded in the correct way.
Problem Set 2 is now due on Friday, 1 February Monday, 11 Feb at the beginning of class.
Thursday, 23 January, read the Unix tutorial Intro, tutorial 1, tutorial 2. No quiz on Monday, 28 January
Problem Set 1 is due on Monday, 28 Jan at the beginning of class.