• Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens during casting  RSS feed

 
Sathish Kumar Govindan
Greenhorn
Posts: 18
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in JLS - 5.1.2 through 5.1.4.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!