ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
def to_signed(v):
    return v - (1 << 32) if v >= 1 << 31 else v
print(to_signed(0xFFFFFFFE), to_signed(0x7FFFFFFF), to_signed(0x80000000))
  1. A. -2 2147483646 2147483648
  2. B. -2 2147483647 -2147483648
  3. C. 3 2147483647 -2147483648
  4. D. -1 2147483647 -2147483648

Correct answer: B. -2 2147483647 -2147483648

Values at or above 2**31 are negative in 32-bit two's complement, so subtracting 2**32 gives the signed value. 0xFFFFFFFE becomes -2, 0x7FFFFFFF stays positive, and 0x80000000 becomes -2147483648.

More Python Programming questions

Exams that ask this topic