• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What happens internally when a long is typecaseted to a byte

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What happens internally when a long is typecaseted to a byte?

Regards,
Raghu
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you mean assigning a byte value to a long variable, this is called widening primitive conversion. No data is lost.

If you mean assigning a long value to a byte variable, this is called narrowing primitive conversion. Data can be lost.

See the JLS: http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:

If you mean assigning a long value to a byte variable, this is called narrowing primitive conversion. Data can be lost.



This is because bytes are allocated less space than longs correct?

And by typecasting I think he meant something like:


Hunter
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is because bytes are allocated less space than longs correct?

Yes
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you cast a long to a byte, like this:

then then lowest 8 bits of the 64-bit long are put into the byte b. The upper 56 bits of the long are discarded (so you lose information).
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you do something like this? if you just wanted to get a byte representation of the long value:



I get these results, but I'm pretty sure they are wrong:

longValue: 12345600
byteValues: 78 0 0 0 0 0 0

Hunter
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know exactly what your intention is with that code. A long is 64 bits, eight times as large as a byte, which is 8 bits. If you want to get the bits of the long into eight bytes, you can do something like this:

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:I don't know exactly what your intention is with that code. A long is 64 bits, eight times as large as a byte, which is 8 bits. If you want to get the bits of the long into eight bytes, you can do something like this:



This code actually causes an array index out of bounds exception.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't test it and I'll leave it as an exercise for you to fix it
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper, I see what my mistake was now. Instead of shifting the bits like you did I was subtracting from the long value.
 
reply
    Bookmark Topic Watch Topic
  • New Topic