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))- A. -2 2147483646 2147483648
- B. -2 2147483647 -2147483648
- C. 3 2147483647 -2147483648
- 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
- What is printed? python s = 'hello world' print(s.capitalize(), s.title().swapcase(), s.center(15, ''), s.zfill(13))
- What is printed? python print(divmod(-7, 2), divmod(7, -2.0), 7 // -2.0, -7 % 2.5)
- What is printed? python x = -17 print(x // 5, x % 5, divmod(x, -5), x.mod(5))
- What is printed? python print(1018 + 1 == 1000000000000000001, 231 - 1 + 1, -(231), (231) (231) == 262)
- What is printed? python s = 'a' for i in range(3): s = s + s[::-1] + str(i) print(s, len(s))
- What is printed? python t = (1, 2, 3) t += (4,) u = t t = 2 print(t, u, t is u, len(t) len(u))