aspose file tools*
The moose likes Beginning Java and the fly likes not cleared Primitive type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "not cleared Primitive type?" Watch "not cleared Primitive type?" New topic
Author

not cleared Primitive type?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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


Creativity is nothing but Breaking Rules
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
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
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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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.

Henry
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not cleared Primitive type?