For each of the following propositions, write in all comparisons that make it always true among the four possibilities:
< > == !=
If none are guaranteed to hold, please indicate that explicitly by marking it with an X. Assume the variables are declared with int x,y; and initialized to some unknown values. You may assume that int’s are 32 bits wide, char’s are 8 bits wide and that right shift is arithmetical on signed numbers and logical on unsigned numbers.
1. If x > y then y ? 0
2. If x < 0 then x+1 X 0
3. If x > 0 then x+1 ? 0
4. If (x >> 31) < 0 then x ? 0
5. If ((x > 31) < 0 then x & 1 ? 0
6. If x > y then (unsigned) x ? y
7. If ((unsigned char)x >> 1) < 64 then (char) x ? 0
Answer :
Let x=5,y=3
1) if x>y the y?0
Ans: 5 > 3 then 3 >0 so
x > y then y > 0
Let x=-2
2) if x < 0 then x+1 X 0
Ans : -2 < 0 then -2+1 < 0
x < 0 then x+1 < 0
Let x=2
3. If x > 0 then x+1 ? 0
Ans : 2>0 then 2+1 > 0
x > 0 then x+1 >0
Let x=-15
4. If (x >> 31) < 0 then x ? 0
Ans: (-15>>31)=-1
-1< 0 then x < 0
5. If ((x > 31) < 0 then x & 1 ? 0
Ans : Let x=32
((x > 31) < 0 then x & 1 == 0
6. If x > y then (unsigned) x ? y
Ans : x > y then (unsigned) x > y
7. If ((unsigned char)x >> 1) < 64 then (char) x ? 0
Ans : Let x= 34
x>>1 => 17 < 64 then x>0
Get Answers For Free
Most questions answered within 1 hours.