Two’s complement is the way most computers represent integers.  The highest bit indicates positive or negative:

0b0xxxxxxx = value is positive or zero

0b1xxxxxxx = value is negative

Convert positive value to negative

Invert the binary digits and add one to the result.

E.g. 1 / 0x01 becomes 0xFE, +1 = 0xFF

Convert negative value to positive

Invert the binary digits and add one to the result.

e.g. -1 / 0xFF  becomes 0x00, +1 = 0x01

Good resources

https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *