We all know that byte Variables can have values from -128 to +127 and occupy 1 byte (8 bits) in memory

so 0 1 1 1 1 1 1 1 => +127 as first digit is 0 which means +ive means max number represented by byte

but what should be the least number..byte defination says it can be -128..I am not able to understand that part

so 128 means

1 0 0 0 0 0 0 0

but here first 1 is used to detemine the sign

In my view least number should be -127 because

1 0 0 0 0 0 0 1 which means -127(2'complement of 127) where first (right most) will deal only with sign and rest is value.

but if somebody will include -128 where can we store the sign of the number in this case as rightmost bit is part of the number and we can only have 8 bit representation
Also 2' complement of 128 and -128 is same.

so 128=>1 0 0 0 0 0 0 0
-128=>2's complement of 128=>1 0 0 0 0 0 0 0

Twos complement is not a simple sign/value representation. By definition, a 1 bit followed by all 0 bits is the lowest value. That's simply how 2s complement was defined to work.

Also 2' complement of 128 and -128 is same.

so 128=>1 0 0 0 0 0 0 0

No. By definition, in 8-bit 2s complement, there is no 128.

0111 1111 = 127. Add one and we get 1000 0000, which 2s complement defines to "wrap around" to the lowest value, which is -128. If we add one to that, we get 1000 0001, which is -127. If we keep counting up, we get to 1111 1110 (-2), 1111 1111 (-1), and then it wraps again to 0000 0000 (0), with the carry out to the 9th bit ignored.

If you don't like it, you can certainly define your own system any way you want. However, there were good reasons for designing 2s complement this way long ago and sticking with it, not the least of which is the simplicity of the arithmetic, as I partially demonstrated above.

when you take two's complement of a number, the steps are:

1) look at the left-most bit. If it is a '1', then the sign is negative, else it is positive.
2) Invert all the bits
3) add 1
4) figure out what the unsigned bit pattern value is
5) apply the sign

So, if we do that:

1) the leftmost bit is 1, so we need to remember the answer is negative
2) we get 0 1 1 1 1 1 1 1
3) we get 1 0 0 0 0 0 0 0
4) the unsigned value of "1 0 0 0 0 0 0 0" is 128.
5) Applying the sign we remember, the answer is -128.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Actually. the way it was worked out was that you need to know how many bits you are using (here 8), and you work out the power of 2 to that many. That is 2 to the 8th, in binary 1_0000_0000.
Then you work out that exactly half the range of numbers ie 1000_0000..1111_1111 is negative, and exactly half the range of numbers ie 0000_0000..0111_1111 is non‑negative.
The for non‑negative numbers, you use the value unchanged, ie same a unsigned.
For negative numbers however, you subtract the value from 1_0000_0000, so -125 is two’s complement of 0111_1101, so you subtract thus:-
1_0000_0000
− 0111_1101 1000_0011
That works for any base of numbers; you can get ten’s complement in 5 digits by subtracting from 100,000 and 50,000-99,999 inclusive are negative.
For binary numbers only, you can change all the bits and add 1 (as shown previously) which always gives the same result as what I showed you.
You can also imagine that the leftmost bit (bit 7) represents -128, bit 6 represents +64, and so on to bit 0 which represents +1. Then add the numbers together. That also always gives the same result, and might only work for binary numbers.

Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28

posted

0

Thanks everyone for your replies

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 42003

31

posted

0

You’re welcome

Remember: two’s complement arithmetic is very simple … when compared to IEEE754 arithmetic!

Jesper de Jong wrote:For a detailed description, see Two's complement on Wikipedia.

That article probably makes the mistake of calling the most significant bit (=MSB =bit₀) a sign bit. Although the MSB determines the sign, it has a value (in the case of bytes, -128), so it is not really a sign bit. In S&M (=sign and magnitude) numbering, which is used by the floating‑point numbers, the bit₀ is a true sign bit; it converts 123.45 to -123.45.
Otherwise it is a good article.

Don't get me started about those stupid light bulbs.