I’ve at least figured out why it was wrong for negative numbers. When the number is negative, I’m supposed to count backwards from the maximum value rather than offset it forward.
Like, 0xff
is -1, 0xfe
is -2 and so on, not 0x80
is -1 and 0x81
is -2.
My bad.