| Author |
What happens during casting
|
Sathish Kumar Govindan
Greenhorn
Joined: Jan 23, 2007
Posts: 17
|
|
Hi all, what happens during the following operation; what will be the value of s? Pleasez, don't show me the output.My doubt is "what happens internally" how the two's complementation of the long value take place. what will happen to the Least significant bits.... Show me the bitwise calculation..... Thanks in Advance...
|
SCJP 5.0, SCWCD JEE5
|
 |
John Dell'Oso
Ranch Hand
Joined: Apr 08, 2004
Posts: 130
|
|
To represent the number 40002 in binary requires 16 bits - 1001110001000010. The long data type is 64 bits - hence no "two's complementation". It's a positive integer - 16 bits well and truly fits into 64 bits. The short data type is 16 bits in length, so when the cast takes place, the binary representation does not change. However, since the leftmost bit is a "1", this number now becomes a negative. To find it's value using two's complement arithmetic, subtract 1 and then flip the bits and you get 25534. Therefore, 40002 (long) is -25534 (short). Not sure what you mean by the "internals". That's how I work these things out in my head (errh .. pen and paper - and perhaps a hex calculator). Regards, JD [ January 23, 2007: Message edited by: John Dell'Oso ]
|
 |
Steve Fahlbusch
Ranch Hand
Joined: Sep 18, 2000
Posts: 492
|
|
Greetings First don't be surprised if someone talks to you about your login name. Also, for common items like this, searching is your friend - what just returned for mine was a reference to the language spec. java language specification (conversion) [ January 23, 2007: Message edited by: Steve Fahlbusch ]
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
Here's my explanation, which is almost the same as John's, but maybe this clarifies a bit with the part "what happens internally". The variable y is a long, which is 64 bits. So the value of y in binary is this: 0000000000000000000000000000000000000000000000001001110001000010 Now, when you cast this to a short, which is 16 bits, the upper 48 bits are simply discarded. So that's exactly "what happens internally" - the upper 48 bits are just thrown away and you end up with the lower 16 bits in the variable s: 1001110001000010 This bit pattern represents the value -25534. Signed integer data types in Java (byte, short, int, long) store numbers in two's complement by definition. So no "magic" happens internally - it just takes the lower 16 bits of the long value and stores it in the short. [ January 23, 2007: Message edited by: Jesper Young ]
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
|
You might be interested in JLS - 5.1.2 through 5.1.4.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
 |
|
|
subject: What happens during casting
|
|
|