I have a small doubt on the max value that a short(data type) variable can take.
Short can accomodate 16 bits, of which the left most bit is a sign bit and the remaining 15 bits represent value.
So the max value that can be accomodated by the short variable is (2^151).
(^ represents the power symbol).
Does Java allow us to manipulate the short variable such that it takes the left most bit as a value bit instead of sign bit?
[ June 16, 2006: Message edited by: Rusty Shackleford ]
"Computer science is no more about computers than astronomy is about telescopes"  Edsger Dijkstra
This is not the correct definition for two's complement but it works.
Now you just add all the values of the bits together. You find that the MSB is always 1 in negative numbers and 0 in zero or positive numbers, but it is not simply a sign; it has a value of its own.
If you go into the API and find a wrapper class for an integer value (eg Short), and find its field called MIN_VALUE and click on that and then click on the link to constant field values, you will find the values, eg in a Short 32768. That is in the case of a short (or Short) 2^15, a figure which is quoted in the class description.
The correct way to work out a number in two's complement is like this:
Now if you add 128 + 64 + 32 + 16 + (0*8=0) + (0*4=0) + 2 + 1, you get 13.
Or, if you work out the two's complement of 13 (1111_0011), you get 0000_1101, which is +13.
Can you change the way Java manipulates the bit values? NO.
The integral types are byte, short, int, and long, whose values are 8bit, 16bit, 32bit and 64bit signed two'scomplement integers, respectively, and char, whose values are 16bit unsigned integers representing UTF16 code units
Then:
# For byte, from 128 to 127, inclusive
# For short, from 32768 to 32767, inclusive
# For int, from 2147483648 to 2147483647, inclusive
# For long, from 9223372036854775808 to 9223372036854775807, inclusive
# For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
So the MSB(or at least one of the bits, most likely MSB) is the sign bit.
"Computer science is no more about computers than astronomy is about telescopes"  Edsger Dijkstra
+1 = 00000001
1 = 10000001
I've used numbers where the only difference between a positive n and negative n is the high order bit. Or was it nibble. The positive and negative range is the same, and negative 0 does not equal positive 0.
In 2s complement the high order bit does indeed indicate a positive or negative number, but it's far from the only difference.
Does that sound right?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
+1 = 00000001
1 = 10000001"
Signed magnitude uses that, but is not commonly found in processors, also BCD(binary coded decimal) might utilize that, not sure. 2's complement uses a "true" sign bit, just like grey code uses a parity bit. That particular bit(MSB) is used for only one thing, sign.
Signed 2's compliment flips each bit(this is called signed 1's compliment) then adds one, except for the MSB. That remains unchanged.
"Computer science is no more about computers than astronomy is about telescopes"  Edsger Dijkstra
No, when you flip your bits, and add 1 the MSB might change. If the MSB is fixed, then you are talking about S+M. There is one instance where the MSB would change in two's complement (what they call a weird number in the Wikipedia article quoted):the MSB . . . remains unchanged.
Using a byte (2^7 = 128 to 0 to 2^71 = 127) because it is easier:
128 = 1000_0000
one's complement = 0111_1111
Add 0000_0001:
0111_1111
0000_0001
1000_0000, which is why you call it a weird number; it has reverted to its original value.
Here are 3 representations of 9 in 8 bits to keep it simple:
Signed magnitude: 10001001
signed 1's: 11110110
signed 2's: 11110111
What is the one thing that does not change? This extends to all negative numbers, the only thing the same is the MSB is always 1. Ditto for positive, except the MSB is 0
Note there is a difference between signed and unsigned 2's complement.
"Computer science is no more about computers than astronomy is about telescopes"  Edsger Dijkstra
Try this number, is S+M:
0101_0101. It should come to 55hex = 85dec. In two's complement it comes to 55hex = 85dec.
Now try this number in S+M:
1101_0101. In S+M the only change is the MSB, which represents sign. So it comes to 55hex = 85dec. But if you try
. . . it most certainly won't come to 85. Try it. I won't tell you the answer.
Stan James is quite right to say
Even though the MSB in a two's complement number is always 1 when the number is negative and 0 if the number is not negative (including zero), it does not follow the definition he gives of a sign bit. The value of a two's complement negative number is not (minus) its n1 allexceptmostsignificant bits.Is it only a true "sign bit" if
+1 = 00000001
1 = 10000001
I think we agree on what we think a two's complement number is, just disagreeing about what you call its bits.
Of course the signed magnitude of a negative number is not the same as 2's complement, it is different for positive numbers as well. You can not compare SM and 2's. You can not just add a 1 in the MSB to convert from positive to negative, I never said that.
+7 is 2's: 0111, if you just change the sign bit, you get 1, and that makes sense, because how 2's is derived. What that does not mean is that the MSB is used for multiple meanings nor that other bits have special significance.
Take the SM of 1: turn it into 2's: 1111. What I was saying is that MSB in signed 2's is used only for sign, nothing else. It is a sign bit. There is no binary code that I am aware of that a bit is used for different things in the same context, that leads to ambiguity. That doesn't mean that simply changing the MSB gives you the opposite value. That happens in SM, not 2's.
"Computer science is no more about computers than astronomy is about telescopes"  Edsger Dijkstra
Yup, yup, yup. Tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
