• 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

casting question

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello...
On page 179 (Kathy&Bert) there is an example of casting a long into a byte:

The result is : the byte is -126
If you look at the bit representation of 130
10000010 = 130
I don't understand after casting the number changes to -126 ???
[ December 29, 2003: Message edited by: Kristof Camelbeke ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A long has 64 bits, a byte 8. When casting a long to a byte you will lose the first 56 bits:
130 as a long = 00000000...000010000010 (64 bits)
cast to a byte = 10000010 (8 bits)
Since the binary representation (two's complement) starts with a 1, this means that the number is negative. To get the decimal value, subtract 1 of the binary representation, invert the bits and put it in decimal representation:
10000010 - 1 = 10000001
after inversion = 01111110
decimal = -126
 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks for the explanation... Hope they don't ask many of these on the exam though (time-consuming)
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kristof Camelbeke:
Ok thanks for the explanation... Hope they don't ask many of these on the exam though (time-consuming)


I wouldn't say time-consuming, check out my post here , i suggested an easy way to solve the problem.
Hope this helps.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic