• 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

Left Shift of byte

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

When a left shift is performed on a byte it is first promoted to an int. When you want the result of the shifted byte value is it true that you must discard the top three bytes of the int result?. Is this because the top three bytes are used only for representing the sign of a byte or a short vaue?

This statement is confusing me because I thought the sign of a byte/short/int was only represented by the right most bit?

Cheers,
John
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sign bit is the left-most bit, not the right-most.
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Ryan:
Hi all,

When a left shift is performed on a byte it is first promoted to an int. When you want the result of the shifted byte value is it true that you must discard the top three bytes of the int result?. Is this because the top three bytes are used only for representing the sign of a byte or a short vaue?

This statement is confusing me because I thought the sign of a byte/short/int was only represented by the right most bit?

Cheers,
John




Thanks Stuart, I meant to say left most bit. Is the statement regarding the top three bytes true??
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you know bytes just have 8 bits. When you shift left the right operand of the shift operator [<<] is promoted to an integer (if it is not a integer already). The right operand is not taken into account for promotion, so it could be a long and would not affect the result.

Now, if the shifted value is assigned back to a byte the upper 16 bits will be discarded.

So, in the next example, when you move the 1 bit to the left, up to the 7th position, then you turn the byte into a negative number.



Hope it helps.
[ June 18, 2005: Message edited by: Edwin Dalorzo ]
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Dalorzo:


Now, if the shifted value is assigned back to a byte the upper 16 bits will be discarded.




Hope it helps.

[ June 18, 2005: Message edited by: Edwin Dalorzo ]




Do you not mean that the upper 24 bytes will be discarded - given that a byte has only 8 bits (32-8 = 24) ??
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, Mr. Ryan. I did the math wrong. Thanks for the correction.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic