unsigned right shift operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    unsigned right shift operator

    Hi,
    can anyone explain the unsigned right shift operator with example?
    And also why there is no unsigned left shift operator? is there any specific reason for that?

    -Thanks & regards,
    Hamsa
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by gaya3
    Hi,
    can anyone explain the unsigned right shift operator with example?
    And also why there is no unsigned left shift operator? is there any specific reason for that?

    -Thanks & regards,
    Hamsa
    Why don't you just write a little test program and see for yourself? Both the
    signed and unsigned right shift operator shift their left operand to the right. The
    signed shift moves a copy of the leftmost bit (the 'sign bit') in as the new leftmost
    bit; the unsigned right shift puts a zero bit there.

    Doing the same for a left shift would be (almost) without meaning as a 'signed
    left shift' because the rightmost bit isn't a sign bit.

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by JosAH
      Why don't you just write a little test program and see for yourself? Both the
      signed and unsigned right shift operator shift their left operand to the right. The
      signed shift moves a copy of the leftmost bit (the 'sign bit') in as the new leftmost
      bit; the unsigned right shift puts a zero bit there.

      Doing the same for a left shift would be (almost) without meaning as a 'signed
      left shift' because the rightmost bit isn't a sign bit.

      kind regards,

      Jos
      This is a good idea. And to see the binary representation of an int, you can use Integer's toBinaryString method.

      Comment

      Working...