00000101 is 5
10000101 is -5
but 10000101 is also 133
I don't understand why 1 binary is able to represent 2 numbers. Any help is appreciated, thank you.
CodePudding user response:
The word “Gift” has at least two meanings. In English, it means a present you give somebody. In German, it means poison. To know which concept a speaker means, you must know which language they are speaking.
The bit string 10000101 does not mean anything by itself. It is just some bits. Bit strings have values only when we associate them with types, which are (in part) methods of associating values with bit strings. To know what value it represents, you must know which type it is being used with.
If we interpret 10000101 as a pure binary numeral, it means 1•27 1•22 1•20 = 128 4 1 = 133.
If we interpret 10000101 as a sign-and-magnitude representation, it means negative with 1•22 1•20 = − (4 1) = −5.
If we interpret 10000101 as a two’s complement representation, it means −1•27 1•22 1•20 = −128 4 1 = −123.
In C, every declared object and every constant has a type, and every expression built from these things has a type. The type says how to interpret the bits.
CodePudding user response:
In these representations of a signed number that occupies one byte
00000101 is 5
10000101 is -5
the most significant bit is the sign bit that determines whether the stored number is positive or negative.
That is if the sign bit is set then the corresponding value stored in value bits is negated.
This representation of signed values is named like sign and magnitude.
It is implementation-defined how signed values are stored. Most computer architectures use the so called 2's complement representation. For such an architecture the negative number -5 is represented like
11111011 is -5
If the number is considered as having an unsigned integer type then no bit is allocated as the sign bit. In this case all bits are value bits. So for unsigned integer this representation 10000101 yields the value 133.
CodePudding user response:
I don't understand why 1 binary is able to represent 2 numbers.
Pure binary numbers have no sign.
So as pure binary, 10000101 is unambiguously 133, pure and simple.
But, of course, we want to be able to handle negative numbers. So there are various ways of rigging things up so that some bit patterns represent negative numbers. But as soon as you do that, you're going to end up with bit patterns which can be interpreted two ways: as straight binary (giving a positive number) or as signed binary (giving a negative number). Any bit pattern that represents a negative number can also be interpreted as a positive number, by just not using whatever negative-number rule you were using.
Here's another way of thinking about it. Consider the word "march". It's the thing that bands do in parades. But if I capitalize the first letter, March, it's the third month of the year. So If I write "March", and I pay attention to the capitalization, it's a month, but if I ignore the capitalization, it's a verb.
Similarly, if I write 10000101, and I ignore the possibility of signedness, I get 133.
But if I pay attention to sign, I get a negative number.
Or if I interpret it as a character, I might get something else!
Here are 5 possibilities:
bit pattern | interpreted as | gives |
---|---|---|
10000101 | pure binary | 133 |
10000101 | sign/magnitude | -5 |
10000101 | ones' complement | -122 |
10000101 | two's complement | -123 |
10000101 | character | à |
(Now, I confess, in the last row I had to cheat, by using the old MS-DOS character set. In Unicode, 10000101 does not represent a character, and in the Windows character set, it's one character that's an ellipsis, or three dots: … .)
Now, one thing you may be worried about is whether we're getting "something for nothing", by having a bit pattern that can do double duty as either as positive or a negative number. Are we cheating and extending the range somehow? The answer is that, no, we're not. Let's stay with 8-bit numbers. If we treat our 8-bit numbers as pure binary, we can cover the range from 0 to 255 (that is, 00000000 to 11111111). We can't represent the number 300, because it takes too many bits, and we can't represent the number -5, because we have no way to represent negative numbers. With 8 bits, in pure binary, we can represent only 0 to 255, and that's it.
If we switch to two's complement, we can represent any number from -128 to 127 — which is exactly 256 different numbers. We still can't represent 300 (positive or negative), because it's still too many bits. But, now, we can't represent the number 200, either, because if we try to, it's 11001000, and that's the two's complement bit pattern for -56. Similarly, we can't represent 133 (your original example), because its bit pattern is 10000101, and that's a negative number, too, -123.