Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting from long to byte

 
Ankit Chandrawat
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the output should be -126....
what is your machine config..???
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont we perform these calculations using Two's complement???
 
sonia jaswal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Peter Ricke explained me this using the Tow's complement... hope it right???
 
Henry Wong
author
Marshal
Pie
Posts: 21379
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so that means we dont flip the binary value and add one to it... is it wrong..???
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic