Converting signed 255 into 8-bit twos complement

signedtwos-complement

I need to convert a signed decimal number (255) into 8-bit two's complement for a HW problem.

My problem is that I'm not sure how to make a number like 255 positive with 8-bits, because I believe the range is -128 to 127, correct?

If 01111111 is 127, then how can I get 255 into binary with just 8 bits.

I think I just don't understand if "signed" is only referring to the decimal number or if it also refers to the binary number.

Best Answer

A signed value of 255 would have to - at minimum - be represented in 9 bits.

0...11111111 = +225

Given an 8 bit representation, you are correct, we can represent only up to +127/-128.

Were we to cast signed 255 to an 8 bit field, we'd lose the semantic meaning of the first bit, leaving us with 11111111 representing -1 (though potentially -0, depending on our implementation).

Attempting to take the two's complement of this value would yield:

 11111111
 00000000 (flip bits)
 00000001 (add one)
=       1 (integer value)

Like I said in the comment, this looks to me like a question I would have been asked to query my understanding of that signed bit and it consuming a bit of numeric representation, which you seem to have figured out already.