File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting from long to byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting from long to byte" Watch "Casting from long to byte" New topic
Author

Casting from long to byte

Ankit Chandrawat
Ranch Hand

Joined: Jan 03, 2008
Posts: 87
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

Joined: Jan 01, 2007
Posts: 184
the output should be -126....
what is your machine config..???


The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
Abhijit Das
Ranch Hand

Joined: Sep 25, 2007
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.


Abhijit Das
SCJP 5.0 | SCWCD 1.5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Jun 01, 2007
Posts: 42
dont we perform these calculations using Two's complement???
sonia jaswal
Ranch Hand

Joined: Jun 01, 2007
Posts: 42
Actually Peter Ricke explained me this using the Tow's complement... hope it right???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Jan 29, 2006
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 .


Thanks & Regards,
ChandraSekharMangipudi
sonia jaswal
Ranch Hand

Joined: Jun 01, 2007
Posts: 42
so that means we dont flip the binary value and add one to it... is it wrong..???
 
 
subject: Casting from long to byte
 
Similar Threads
casting shifted values
question on maximum value of primitive data type
Casting
Primitive Casting
Casting