This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

byte to integer conversion -- mock exam qtn

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the result of the following ..
(byte)0x81 >> 2
the answer given is FFFFFFE0. I do understand that before any shift operation , it will be converted to Int. How come byte 0x81 becomes int 0xFFFFFF81 ? can anyone explain this?
Baskaran.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey what a coincidence I was just about to tyoe the same question.I will wait for the answer too.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although you cast 0x81 to byte, it still gets promoted to int before shift. As the highese bit of 0x81 is 1, when it's promoted to int, it takes the 1 and propagates it all the way to the leftmost bit, which oxFFFFFF81.
Hope that helps.
 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doet it mean when a byte is promoted to an integer , if it is filled with 0 or 1 depends on the leftmost bit??
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i couldn't get u..
can u tell me how could u arrive at oxFFFFFF81 in detail..
thanks

Originally posted by Tian:
Although you cast 0x81 to byte, it still gets promoted to int before shift. As the highese bit of 0x81 is 1, when it's promoted to int, it takes the 1 and propagates it all the way to the leftmost bit, which oxFFFFFF81.
Hope that helps.


 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
I think there may have been a mistake somewhere that propagated through this post.
The original question is:
"what is the result of the following .."
(byte)0x81 >> 2
The correct answer is 0xFFFFFFE0 (NOT 0xFFFFFF81).
You can test it for yourself with the following little program:

Here are the steps to get this result:
0x81 (hexadecimal) is equal to 129 (decimal) which is
10000001 (binary).
So there is the byte value.
Next, before the right shift occurs, the byte is promoted to an int. Because the most significant bit in the original byte is a 1, 1's are used to fill in the upper bits of this integer. So we get this:
11111111 11111111 11111111 10000001
Now we do our right shift by 2, and we get this:
11111111 11111111 11111111 11100000
Finally, we convert the binary to hexidecimal:
1111 1111 1111 1111 1111 1111 1110 0000
F F F F F F E 0
So the answer is:
0xFFFFFFE0
Hope this helps.
Stephanie
 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks stephanie
 
Baskaran Subramani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks stephan.. I got it..
 
I'm so happy! And I wish to make this tiny ad happy too:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic