Question

enter image description here

Answer is c). I understand that the expression evaluates to true but what does signed/unsigned have to do with booleans?

Was it helpful?

Solution

It means your book is either (a) misleading, (b) poorly worded, or (c) wrong.

Case (a) would be if the text is talking about how relational operators are implemented in CPU architecture. In many if not all architectures, performing a compare instruction sets processor flags for the output of both the signed and unsigned comparison, and then conditional jump instructions can be used to branch to different code based on the values of the different flags. The processor flags are single bits and you don't perform arithmetic on them; asking whether the flags are signed or unsigned is meaningless.

Cases (b) and (c) are if the text is talking about a specific programming language. This would almost certainly have to be C, based on the syntax and talking about comparisons returning integers.

Case (b) would be if the text is asking whether the arguments of the comparison are signed or unsigned. In C, expressions that mix signed and unsigned generally convert the arguments to unsigned first, so the comparison being performed is on two unsigned values. But this is very poor question wording (even though I strongly suspect this was the author's actual intention).

Case (c) would be if the text is asking whether the result of the comparison is signed or unsigned. The result of a comparison in C is a signed int. C11 (ISO/IEC 9899:201x) §6.5.8 Relational operators says:

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

Licensed under: CC-BY-SA with attribution
Not affiliated with cs.stackexchange
scroll top