0
\$\begingroup\$

One way to convert any number to 2's complement is by adding the number to 256, if the number can be represented by 8 bit number. For example if we want to convert -100 to 2's complement we do addition with 256 to get 156, which has a binary value of 10011100, but if we do binary conversion while considering this as a 2's complement number we will get -128+16+8+4 = -100. If seems to be working as I am getting wring answers, but I am not able to figure out why this works, what the math/logic behind the adding a 8 bit number. Any help/direction will be appreciated.

\$\endgroup\$
0
\$\begingroup\$

What you describe is how 2's complement is designed: if you start at 0 and add 1, you get the value 1, if you substract 1, you get the value -1. 0b0000_0000 - 1 = 0b1111_1111 (which is 255 when interpreted as unsigned). Hence you can calculate 0b1_0000_0000 - N to get ( - N ).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.