Hi All, 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^15-1). (^ 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?

The only unsigned primitive type in Java is char. Since a char is an integral value(number), you can use that to store un unsigned short value. It would get tedious to keep having to convert to short/int and then back again for arithmetic operations. Probably not worth the hassle. [ June 16, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra

But Java doesn't use sign bits in integers numbers. Sign bits are used in S+M formats, and Java uses two's complement. Contrary to what it says in the Wikipedia article, the MSB (= most significant bit, or leftmosr bit) is a value bit. It is easiest to understand in a byte, because it only has eight bits, numbered from 0 on the right (LSB = least significant bit) to 7 on the left.

This is not the correct definition for two's complement but it works.

Bit 0: 2^0 = 1.

Bit 1: 2^1 = 2

Bit2: 2^2 = 4.

Bit 3: 2^3 = 8

Bit 4: We are now in the left half of the byte: 2^4 = 16

Bit 5: 2^5 = 32

Bit 6: 2^6 = 64.

Bit 7: MINUS 2^7 = MINUS 128.

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:-

Take a number, eg 13.

Work it out in binary, eg 1101.

Give it enough digits to fit into the appropriate number of bytes. Let's stick to eight to make it easier. 0000_1101

Invert every digit. That give's you its one's complement, but you hardly ever use one's complement. 0000_1101->1111_0010.

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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357

23

posted

0

The remainder of the Wikipedia article here appears accurate, apart from calling teh MSB a sign bit when it has a value of its own.

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490

posted

0

In 2's complement the MSB is the sign bit and this is what java uses. From the JLS:

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 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.

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

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490

posted

0

"Is it only a true "sign bit" if

+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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357

23

posted

0

I think we agree about what two's complement and S+M (as described by Stan James = sign & magnitude) are. Just that some of us are not happy calling the MSB a "sign bit" when (as Stan James points out) it does so much more.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357

23

posted

0

the MSB . . . remains unchanged.

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):-

Using a byte (-2^7 = -128 to 0 to 2^7-1 = 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.

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490

posted

0

in signed 2's complement the sign bit stays the same. MSB is used for only one thing in signed binary number, sign. One bit for sign, the rest for the value. In unsigned 2's complement, it can and usually will change.

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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357

23

posted

0

Rusty, are you sure? As far as I can remember, there is only one sort of two's complement number, which is signed. 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

Is it only a true "sign bit" if

+1 = 00000001 -1 = 10000001

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 n-1 all-except-most-significant bits.

I think we agree on what we think a two's complement number is, just disagreeing about what you call its bits.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357

23

posted

0

I am afraid the code I posted recently won't work. Try this instead:-

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490

posted

0

There is signed and unsigned 2's complement, although signed is far more common.

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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357

23

posted

0

Thank you Rusty Shackleford.

I think we agree about what we are talking about, but just are using different names for it.