Win a copy of Head First Agile this week in the Agile forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Casting from long to byte

Ankit Chandrawat
Ranch Hand
Posts: 88
Hi all,

This is from K&B book...chapter#2,page no 185. I tried to complie the following program:-

class Casting {
public static void main (String[] args) {
long l = 130L;
byte b = (byte)l;
System.out.println("the byte is "+b);
}}

output( i am getting):130
output(according to the book):-126
According to the book the point to be noted here is that a value larger than 127 is being cast to byte.But the output i am getting is the same as opposed to any other value.I also tried compiling the above program taking different values for l.But everytime the output is quite normal and doesnt throw any surprises regardless of the value is greater than 127 or not.Please help.

abhishek pendkay
Ranch Hand
Posts: 184
the output should be -126....

Abhijit Das
Ranch Hand
Posts: 156
range of byte is
-128 -127 -126 ........ 0 1 2 .............126 127

So,
byte b = 130;
so, it crosses by 3 position. So , answer will be -126( from reverse)
e.g. byte b = 128 , answer is -128
byte b = 129 , answer is -127
and so on.
I think it is cleared.

Campbell Ritchie
Marshal
Posts: 55799
164
Ankit Chandrawat, welcome to the Ranch.

Please check what you are getting; I copied and pasted your code and got -126, which is what I would have expected.

sonia jaswal
Ranch Hand
Posts: 42
dont we perform these calculations using Two's complement???

sonia jaswal
Ranch Hand
Posts: 42
Actually Peter Ricke explained me this using the Tow's complement... hope it right???

Henry Wong
author
Sheriff
Posts: 23284
125
Originally posted by sonia jaswal:
dont we perform these calculations using Two's complement???

Casting from a long to a byte is done simply by truncating -- assigning the lower byte of the long variable to the byte variable.

Henry

Raghavan Muthu
Ranch Hand
Posts: 3389
Howdy Ankit Chandrawat,

I also get the same '-126' as the output. Could you please double check it? I am sure it would be the same with any version of JRE.

Raghavan Muthu
Ranch Hand
Posts: 3389
Originally posted by sonia jaswal:
Actually Peter Ricke explained me this using the Tow's complement... hope it right???

Two's complement is done when you do any arithmetic operation which involves negative sign bit.

Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
HI. To know the Byte value :Add positive and negative numbers+0(128+127+1)is 256.Now subtract the 256-130=126.Now we have to negate the
output, so we get -126. Which is the final output .

sonia jaswal
Ranch Hand
Posts: 42
so that means we dont flip the binary value and add one to it... is it wrong..???

 It is sorta covered in the JavaRanch Style Guide.