BitNot does not flip bits in the way I expected
twosComplement[x_, n_] := UnitBox@IntegerDigits[x, 2, n]
twosComplement[35, 8]
{1, 1, 0, 1, 1, 1, 0, 1}
I don't think there is a built-in function to generate the two's complement representation. Easy to implement though.
twosComplement[x_, n_] := IntegerDigits[2^x - n, 2, n]
twosComplement[35, 8]
(* {1, 1, 0, 1, 1, 1, 0, 1} *)
Without using IntegerDigits[]
:
With[{n = 34},
{n, BitXor[BitShiftLeft[1, BitLength[n]] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11101₂}
With[{n = 34, p = 8},
{n, BitXor[BitShiftLeft[1, p] - 1, n]} // BaseForm[#, 2] &]
{100010₂, 11011101₂}