If we have a binary sequence that is \$x\$ bits long, what are the minimum and maximum that can be represented in two's complement form?
The maximum is \2ドル^{x-1}-1\,ドル and the minimum is \$-2^{x-1}\$.
But why? How can it be shown that this is the case?
4 Answers 4
Look at 3 bits representation:
000 = 0
001 =わ 1
010 =わ 2
011 =わ 3
100 =わ -ひく4
101 =わ -ひく3
110 =わ -ひく2
111 =わ -ひく1
Half of the numbers are starting with '1', which is the sign bit. So half of the numbers are negative. It means that we have \$(2^x)/2 = 2^{(x-1)}\$ negative numbers. The rest are positive and zero.
For x bits, there are 2x values.
In one's complement system there are two zeros and 2x-1 - 1 positive and the same number of negative values.
In two's complement system those are assigned so that there is only one zero and one more negative value than there are positive values.
The two's complement system is in use, because it stems from how simple hardware naturally operates. Think for example you car's odometer, which you have resetted to zero. Then put the gear on reverse, and drive backwards for 1 mile (Please don't do this in reality). Your odometer (if it's mechanical) will roll from 0000 to 9999. The two's complement system behaves similarly.
enter image description here
Edit: Added the picture to harvest an amazing amount of points from this question
-
\$\begingroup\$ Odometer usually don't work that way except perhaps on very old cars. Authoritative reference: Ferris Bueller's Day Off (and lots of internet posts). \$\endgroup\$Spehro 'speff' Pefhany– Spehro 'speff' Pefhany2014年12月16日 22:00:34 +00:00Commented Dec 16, 2014 at 22:00
-
\$\begingroup\$ @SpehroPefhany, Works like it in my 1994 Nissan :D \$\endgroup\$PkP– PkP2014年12月16日 22:01:30 +00:00Commented Dec 16, 2014 at 22:01
For a binary sequence of X bits there are 2^x possible values.
Using unsigned integers to represent these then, you have the set from 0, 1, ...(2^x)-1. If one wanted to effectively shift the values down to have half negative and half positive, you would subtract 2^(x-1) (which is half the range) from every value, this giving you 0 - 2^(x-1),....,(2^x)-1 - (2^(x-1)).
Reorganizing the later term into 2^x- 2^(x-1) - 1 and recognizing, that y^x - y^(x-1) = y^(x-1), you then have your range, -(2^(x-1))...2^(x-1)-1.
This is done in implementation by negating the MSB, which by virtue of it's place value is half the range.
-
\$\begingroup\$ I made a mistake. y^x-y^(x-1) = y^(x-1) only works when y=2. it cant be generalized to every integer. 2^4 - 2^3 = 2^3, but only because the base is 2. 3^3 = 3^2 != 3^2 \$\endgroup\$Jotorious– Jotorious2014年12月16日 23:08:59 +00:00Commented Dec 16, 2014 at 23:08
In two's compliment, the most significant bit (furthest left) is reserved for the sign of the number, so that's why it's x-1. The reason that it is \2ドル^{x-1} -1\$ for positive numbers is that one of these is used for zero, while in the negative numbers, we start right out at -1.
-
\$\begingroup\$ This is not right. In twos complement the MSB holds the negative of the value of the highest place value. 1011 is -8 +たす 0 +たす 2 +たす 1 =わ -ひく5. twos complement is identical to unsigned binary, except the MSB is negated. \$\endgroup\$Jotorious– Jotorious2014年12月16日 21:18:28 +00:00Commented Dec 16, 2014 at 21:18
-
\$\begingroup\$ Kypalmer, @Jotorious, You're both right, although kypalmer's explanation is a bit unusual (which I actually like a lot) :D \$\endgroup\$PkP– PkP2014年12月16日 21:25:00 +00:00Commented Dec 16, 2014 at 21:25
-
\$\begingroup\$ I think you're meaning \2ドル^{x-1}\$ there, aren't you? \$\endgroup\$user17592– user175922014年12月16日 21:45:51 +00:00Commented Dec 16, 2014 at 21:45
-
\$\begingroup\$ Thanks, Yes that should have been in the exponent. \$\endgroup\$kypalmer– kypalmer2015年01月23日 01:26:26 +00:00Commented Jan 23, 2015 at 1:26
42
inside.. \$\endgroup\$