Numbers and bits · 7 / 15 Exercise Medium

Two's complement

Objective

Computers store negative integers in two's complement: with n bits, the value -x is stored as 2**n - x. The result is the same as keeping the lowest n bits of x.

Instructions

  • The first line has the number t of cases. Each case is a line with bits (number of bits) and an integer x that fits in that number of signed bits.
  • Show x in two's complement with exactly bits bits (leading zeros included).
Input
3
8 5
8 -1
4 -8
Output
00000101
11111111
1000

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise