This week's giveaway is in the Spring forum. We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line! See this thread for details.

Hi Friends
In java as you know in primitive types the left most bit represents sign of a number for ex:Byte contains maximum number up to +127 if you type cast 128 to byte it will give you -128 because it will take two's complement of 128 and it will give you-128 my doubt is if we assign positive numbers to primitive data types it will store that number in two's complement number or how it gets stored the positive number....... Please clear my doubt with example

In java as you know in primitive types the left most bit represents sign of a number for ex:Byte contains maximum number up to +127 if you type cast 128 to byte it will give you -128 because it will take two's complement of 128 and it will give you-128

Just nickpicking here, but there is no "take two's complement" during a cast. The lower eight bits of an int is simply stored into the byte. Nothing else is done.

my doubt is if we assign positive numbers to primitive data types it will store that number in two's complement number or how it gets stored the positive number....... Please clear my doubt with example

A positve 128 can never be stored into a byte. so no... you can't store that value.

Regardless, can you elaborate on your question? are you asking why 128 can't be stored into a byte? Or is this a question about two's complement?

It's also not quite accurate that the highest order bit in a complement number represents the sign. It represents a value, which is taken as negative.

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

posted

0

Henry Wong wrote:

In java as you know in primitive types the left most bit represents sign of a number for ex:Byte contains maximum number up to +127 if you type cast 128 to byte it will give you -128 because it will take two's complement of 128 and it will give you-128

Just nickpicking here, but there is no "take two's complement" during a cast. The lower eight bits of an int is simply stored into the byte. Nothing else is done.

my doubt is if we assign positive numbers to primitive data types it will store that number in two's complement number or how it gets stored the positive number....... Please clear my doubt with example

A positve 128 can never be stored into a byte. so no... you can't store that value.

Regardless, can you elaborate on your question? are you asking why 128 can't be stored into a byte? Or is this a question about two's complement?

Henry

we can store 128 by casting it into byte but it will goes to negative ..because byte can take only +127 as maximum value
my question is if we do like that it will take two's compliment form of 128 and hence stored in memory... two's complement 0f 128 is -128 ... similarly what about the positive values getted stored.... is JVM depends on sign bit to take two's complement of a number or what condition it will check?

my question is if we do like that it will take two's compliment form of 128 and hence stored in memory... two's complement 0f 128 is -128 ... similarly what about the positive values getted stored.... is JVM depends on sign bit to take two's complement of a number or what condition it will check?

Two's complement is something that you, as a human needs to do, in order to understand what number is being stored. The bit pattern (of the lower eight bits) of an int for 128 is the same as the bit pattern of -128 for a byte. The JVM doesn't do anything to the number, it just stores it.

Imagine the lowest eight bits of an int. If they are a zero followed by seven ones, that's 127. If you add one, you get a one followed by 7 zeros, and that's 128.

Now, imagine those eight bits are a byte instead. A zero followed by seven ones is 127; adding one gives you the same 1 followed by 7 zeros, but now it's interpreted as -128. It's the same exact bits.

So if the JVM just plain copies the lowest eight bits from the int to the byte, you see how 128 is automatically converted to -128 with no thinking whatsoever.