80 likes | 192 Views
Quiz 2. Question 1. What is -10 10 in 5 bit 2’s complement notation? First determine what +10 2 is It’s 1010 (1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 ) Pad it with zeroes to 5 bits: 01010 Then flip the bits 01010 flipped is 10101 And finally add 1 10101 + 1 = 10110
E N D
Question 1 • What is -1010 in 5 bit 2’s complement notation? • First determine what +102is • It’s 1010 (1 * 8 + 0 * 4 + 1 * 2 + 0 * 1) • Pad it with zeroes to 5 bits: 01010 • Then flip the bits • 01010 flipped is 10101 • And finally add 1 • 10101 + 1 = 10110 • If you want to check your answer reverse the process and you should end up with 01010
Informal Conversion Algorithm • Say you want to convert 70110 to binary • Find the largest power of 2 that 701 is divisible by • It’s 512 (1,2,4,8,16,32,64,128,256,512,1024,2048,...) • Write down 1 • Subtract 512 from 701 = 189 • Go through the lower powers of 2 one by one • If the power of 2 is less than the number repeat • Otherwise write down a 0 and move on
Example • 701 – 512 = 189 • 1 • 256 > 189 • 10 • 189 – 128 = 61 • 101 • 64 > 61 • 1010 • 61 – 32 = 29 • 10101 • 29 – 16 = 13 • 101011 • 13 – 8 = 5 • 1010111 • 5 – 4 = 1 • 10101111 • 2 > 1 • 101011110 • 1 – 1 = 0 (so we are done) • 1010111101
Question 2 • What do the printf statements shown below print? • int x = 7; • x--; • printf(“%d”,x); • 6 • printf(“%d”,20 / 3); • 6
Question 3 • What does the printf statement shown below print? • if(3 > -7 && 12 < 15) • printf(“true”); • else • printf(“false”); • true
Question 4 • Write a complete program that prompts the user to enter two numbers and that prints the remainder of the first divided by the second. • #include“stdio.h” • int main() • { • int dividend, divisor; • printf(“Please enter the dividend: ”); • scanf(“%d”, ÷nd); • printf(“Please enter the dividend: ”); • scanf(“%d”, &divisor); • printf(“remainder = %d”, dividend % divisor); • return 0; • }
Question 5 • Write a function to return 1 if its single integer parameter is even, and 0 otherwise • inteven(int x) • { • if(x % 2 == 0){ • return 1; • }else{ • return 0; • } • }