• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

byte casting

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is it that System.out.println((byte)128)
gives -128 as output?
128 is beyond the range of byte, so calcualtion will proceed as 128%256.
But what about the -ve sign?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte 8 bit
max value it can hold " 0 1 1 1 1 1 1 1 "

8th bit is sign bit & value is +127

128 default data type is int (32 bytes)
128 ==> "0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 - 1 0 0 0 0 0 0 0"

cast it(32 bits) to byte(8 bits) i.e pick 8 bits from right side i.e
1 0 0 0 0 0 0 0

Please convert this value to decimal value.


I am not in touch with this (shift operator & conversions), pls. wait for better response.
[ November 27, 2004: Message edited by: sanjeevmehra mehra ]
 
It's never done THAT before. Explain it to me tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic