Reputation: 23
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.
Upvotes: 0
Views: 7384
Reputation: 1191
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.
Upvotes: 1
Reputation: 597
In 8bit stands for byte and it is unsigned and and the range that covers is from 0 to 255 so 255 decimal should be 11111111 in 8bit
Signed means that negative number are also allowed if you have to cover the range from -255 to 255 you can put the negative number in one byte and the positves in the other byte
Upvotes: 0
Reputation: 657
I'm definitely not an expert in this sort of stuff but are you sure you have the question correct?
Surely signed refers to the binary number only?
Upvotes: 0