Consider the following two statements regarding ANSI C evaluation:
\textbf{Assertion (A):} The decimal value of the expression (char)( ('~' | 'a') + 50 ) is .
\textbf{Reason (R):} The bitwise OR of '~' (126) and 'a' (97) yields 127. Adding 50 gives 177, which exceeds the maximum signed char value of 127. The explicit (char) cast truncates the integer result to 8 bits, wrapping around to due to two's complement representation.
Which one of the following options is correct?
A
Step-by-Step Solution
Key idea: Integer promotion occurs during arithmetic, but an explicit cast to char forces truncation to 8 bits, invoking signed overflow behavior.
Step 1: Evaluate the bitwise OR. The ASCII value of '~' is 126 (0111 1110). The ASCII value of 'a' is 97 (0110 0001).
.
Step 2: Evaluate the addition. Due to integer promotion, the addition is performed as an int, yielding .
Step 3: Apply the explicit cast. The expression casts the int 177 to char. In 8-bit two's complement, 177 is 1011 0001.
Step 4: Interpret as signed char. The most significant bit is 1, indicating a negative number. The value is .
Step 5: Verify the statements. Assertion (A) correctly states the final value is . Reason (R) correctly identifies the intermediate sum (177), the boundary limit (127), and the mechanism of two's complement wrap-around via casting. R perfectly explains A.
Answer: Both A and R are true, and R is the correct explanation of A.