• 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

>> shift question

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question: What is the result of the following fragment of code?
(byte)0x81 >> 2
A. 0x20
B. 0x3FFFFFE0
C. 0xE0
D. 0xFFFFFFE0
Answer is D. I understand the complier will widen the byte to an int before performing the shift, but could not figure out the result. Please help me. Many thanks.
Moya
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(byte)0x81 >> 2
0x81 = 1000 0001
It is first converted to an int before the shift.
11111111 11111111 11111111 10000001
After the shift it is as follows.
11111111 11111111 11111111 1110 0000 = 0xFFFFFFE0
 
Moya Green
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
Thank you very much. I was stuck in the step to modify
1000 0001 -->11111111 11111111 11111111 10000001. I thought "0" was added before 1000 0001. Anyway, I understand it now. I appreciate your help!
Moya
reply
    Bookmark Topic Watch Topic
  • New Topic