• 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

byte range

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte x=64;
byte y=5;
byte z= (byte)(x*y);

What is the value of z? The answer is 64, I am not getting how it is 64. I think if (x*y) goes out of byte range, output will be 0. Please can anyone explain?
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer 64 is correct ,as byte limit is 128 only.
320 is out of limit so
320-128-> 192(of -ve side) again out of limit
so goes to +ve side, 192-128->64 of +ve
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

64 * 5 is 320

320 in binary is 101000000

Since byte cannot accomodate anything greater than 127 it truncates any leftmost bits that is more than 8 bits counting from right

Hence it retains 01000000, and this is equivalent to 64
Hence you get 64
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
byte x=64;
byte y=5;
byte z= (byte)(x*y);



64 * 5 = 320

The JVM treats the results of multiplications as an int, so as it does the math, it's treating it as an int in memory. So far, no problem.

In Binary, 320 = 101000000, but, a byte can only use 8 of those, so everything after the first eight digits is truncated with the cast, giving you: 01000000

If you then turn 01000000 into decimal, you get 64.

So, that's what's going on under the covers.

Pretty kewl, eh?

-Cameron McKenzie
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shoot....As I responded, there were no other responses.

You've got to be fast on this board. Just to many people smarter than me trying to help out.

-Cameron McKenzie
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cameron McKenzie:
Shoot....As I responded, there were no other responses...


That happens to me a lot. Especially when I spend time going back and forth about how much detail to include in my response.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cats and Mice and Bits:
http://www.javaranch.com/campfire/StoryBits.jsp

Bu.
reply
    Bookmark Topic Watch Topic
  • New Topic