So here's a challenge. Given a bitarray represented by a Python integer (e.g 0x12345678) work what it's two's complement value is (assuming a 32-bit bitarray).

e.g. 0x00000001 => 1

0xffffffff => -1

0x00000001 => -2

0x7fffffff => 2147483647

0x80000000 > -2147483648

Despite all of Python's integers being based on 2s complement form, it turns out to be surprisingly tricky.

One hack is just to use ctypes.c_int32(0x12345678).value, but this feels a little dishonest.

I worked out a way using `value |= (-1 ^ 0xffffffff)` to sign-extend when the high-bit is set, but it's still pretty long and clunky and relies on a conditional statement.

e.g. 0x00000001 => 1

0xffffffff => -1

0x00000001 => -2

0x7fffffff => 2147483647

0x80000000 > -2147483648

Despite all of Python's integers being based on 2s complement form, it turns out to be surprisingly tricky.

One hack is just to use ctypes.c_int32(0x12345678).value, but this feels a little dishonest.

I worked out a way using `value |= (-1 ^ 0xffffffff)` to sign-extend when the high-bit is set, but it's still pretty long and clunky and relies on a conditional statement.