• 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 shifted values

 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am confused about this code

the output for line 1 is 1280 (which is okay).
on line 2 there is a downcast from integer to byte.
the output for line 3 is 0 which is confusing me.

providing this code

it is clear that the output is -126 becasue it actually starts counting up from �128 when we go over 127.
if this is the case then how do you explain the first code with the output of ZERO, isn't it suppose to start counting from �128???
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hallo
y = (byte) (y<<7);//line 2
before y =10
in binary : 00001010
java promote y to int before shift
so the representation of y is
00000000 00000000 00000000 00001010
now the right shift gives
00000000 00000000 00000101 00000000
(4) (3) (2) (1)
the cast to byte take the first 8 bits coressepending to (1) which have the value of 0

hope it helps
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, you're right... hhmmmm, i should've seen that... thx for the help
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic