posted 10 years ago

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

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

Certified on SCJP 5.0, OCPJWCD, Spring3.2, OCEJWSD

John Dell'Oso

Ranch Hand

Posts: 130

posted 10 years ago

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 ]

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 ]

posted 10 years ago

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 ]

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 ]

posted 10 years ago

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

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 ]

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 ]

posted 10 years ago

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

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |