Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Quiz 2

Question 1
What

is -1010 in 5 bit 2s complement


notation?
First determine what +102 is
Its 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
Its 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

Example
701

512 = 189

256

> 189

10

189

128 = 61

101

64

> 61

1010

61

32 = 29

10101

29

16 = 13

101011

13

8=5

1010111

4=1

10101111

>1

101011110

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
#include
stdio.h
the
second.
int main()
{
int dividend, divisor;
printf(Please enter the
scanf(%d, &dividend);
printf(Please enter the
scanf(%d, &divisor);
printf(remainder = %d,
return 0;
}

dividend: );
dividend: );
dividend % divisor);

Question 5

Write a function to return 1 if its single


integer parameter is even, and 0
otherwise
int even(int x)
{
if(x % 2 == 0){
return 1;
}else{
return 0;
}
}

You might also like