• 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 Operator

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding-

Right shift operator
Right shift operator is signed. That means it can be positive or negative.

a) Am i right with the answer?

b) As we always add '0'so there is no question of being signed or unsigned just as the unsigned right shift. Only the case if the number what ever given to be shifted right is itself negative then only the answer will be negative. ex -4>>2 then ans will be (-) irrespective of the calculation.
Am i right?

Left Shift Operator-

Left shift operator is signed. That is what ever the left most bit of the real number is ,that bit only get filled from right side means 0 or 1 and it get the sign as per it if 0 the positive or negative. Am i right?



a)How we calculate the answer for this?I am really not able to find that's why asked?

b)Also if

what will be the effect of changing int to byte?

c)How ever theory says that shift operators only used on integral numbers then how come byte?

I am sorry for such a long post but these questions are driving me crazy. Please help me with these.

Thanks a lot for your patience to read such a long post and also for replying me.

Sandhya
[ February 27, 2008: Message edited by: sandhi mridul ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sandhi mridul:
...these questions are driving me crazy...


Which version of the exam are you studying for? Bit shifting is on the 1.4 version of the exam, but not 1.5 or 1.6. So unless you are preparing for 1.4, you shouldn't have to worry about this. (That's not to say you shouldn't learn it. But if your exam date is coming near, it might not be the best use of time.) But in response to your questions...

Operands are first promoted to the widest type, but always to at least int.

Left, right, and unsigned right...
  • When shifting to the left (<<) , new bits on the right are zero.
  • When shifting to the right (>>) , new bits on the left are the same as the most significant bit (the left-most bit), thus maintaining the same sign.
  • The unsigned right shift (>>>) always inserts zeros as new bits on the left.
  • The right operand...
  • Shifts on type int use only the last 5 bits of the right operand (so it's always between 0 and 31, even if right operand is negative).
  • Shifts on type long use only the last 6 bits of the right operand (so it's always between 0 and 63).

  • [ February 27, 2008: Message edited by: marc weber ]
     
    Ranch Hand
    Posts: 694
    Mac OS X Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    marc knows what he is talking about but I want to add that, in Java, all int types: byte, short, int, and long are all signed integers. That is, they are all two's complement numbers. (in case you didn't know).

    Two's Complement:
    http://en.wikipedia.org/wiki/Two%27s_complement
    [ February 27, 2008: Message edited by: Kaydell Leavitt ]
     
    Author
    Posts: 986
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by sandhi mridul:

    Left Shift Operator-

    Left shift operator is signed. That is what ever the left most bit of the real number is ,that bit only get filled from right side means 0 or 1 and it get the sign as per it if 0 the positive or negative. Am i right?




    No, after shifting it would be:
    0000 0000 0000 0000 0000 0000 1110 1000

    I'm not sure what exactly you are asking in the first
    quoted paragraph, but << simply shifts bits leftword,
    pushing zeros on to the right. The sign bit of the
    result will be whatever bit happens to get shifted
    there. So if you are left-shifting by 1 bit then the
    sign of the result is determined by the bit next to
    the sign bit of the left operand.

    before shifting left by one bit:
    0100 0000 0000 0000 0000 0000 1110 1111 // positive

    after shifting left by one bit:
    1000 0000 0000 0000 0000 0001 1101 1110 // negative


    Originally posted in marc weber's signature:

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer



    Cute analogy, but check out the American Crossword Critics Association's 2007 awards for achievement in puzzle construction.
    [ February 27, 2008: Message edited by: Brian Cole ]
     
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right shift operator
    Right shift operator is signed. That means it can be positive or negative.

    code:
    ---------------------------------------------------------------------------
    But when we do 9>>2the answer is 2

    9/2 ^
    4-1 |
    2-0 |
    1-0 |
    ----
    9-->1001
    1*2^3+0*2^2+0*2^1+1*2^0 = 9

    8+0+0+1=9.

    This is the way of calculation.

    Bit representation- 0000 0000 0000 0000 0000 0000 0000 1001
    As we always add 0 from the left so-
    0000 0000 0000 0000 0000 0000 0000 0010 (two digits are cancel)
    0010
    0+0+2+0=2.
    Answer 2

    --------------------------------------------------------------------------


    a) Am i right with the answer?

    b) As we always add '0'so there is no question of being signed or unsigned just as the unsigned right shift. Only the case if the number what ever given to be shifted right is itself negative then only the answer will be negative. ex -4>>2 then ans will be (-) irrespective of the calculation.
    Am i right?

    Left Shift Operator-

    Left shift operator is signed. That is what ever the left most bit of the real number is ,that bit only get filled from right side means 0 or 1 and it get the sign as per it if 0 the positive or negative. Am i right?


    code:
    ---------------------------------------------------------------------------

    int a=29;a=a<<3;
    0000 0000 0000 0000 0000 0000 0001 1101
    After shifting 0000 0000 0000 0000 0000 0000 1110 1000(three digits(0) are adding in righthand side.

    ans:232.
     
    Ranch Hand
    Posts: 274
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here is a link to read:

    http://www.sap-img.com/java/java-bitwise-shift-operators.htm

    to execute and see results online:
    http://www.geocities.com/gnashes30/java/applets/binary_shift.htm

    To read here:

    Operators and Assignments - Shift Operators

    * << left-shift, >> right-shift, >>> unsigned right-shift
    * only used on integer values
    * binary numeric promotion is not performed on the operands; instead unary promotion is performed on each operand separately (JLS �15.19)
    * both operands are individually promoted to int if their type is byte, short or char
    * a long shift operator does not force a left-hand int value promotion to long (JLS�5.6.1)
    * left-associative
    * left-hand operator represents the number to be shifted
    * right-hand operator specifies the shift distance

    value << 2 // 2 is the distance to be shifted

    * when the value to be shifted (left-operand) is an int, only the last 5 digits of the right-hand operand are used to perform the shift. The actual size of the shift is the value of the right-hand operand masked by 31 (0x1f). ie the shift distance is always between 0 and 31 (if shift value is > 32 shift is 32%value)

    35 00000000 00000000 00000000 00100011
    31 -> 0x1f 00000000 00000000 00000000 00011111
    & -----------------------------------
    Shift value 00000000 00000000 00000000 00000011 -> 3

    -29 11111111 11111111 11111111 11100011
    31 -> 0x1f 00000000 00000000 00000000 00011111
    & -----------------------------------
    Shift value 00000000 00000000 00000000 00000011 -> 3

    * when the value to be shifted (left-operand) is a long, only the last 6 digits of the right-hand operand are used to perform the shift. The actual size of the shift is the value of the right-hand operand masked by 63 (0x3D) ie the shift distance is always between 0 and 63 (if shift value is greater than 64 shift is 64%value)
    * the shift occurs at runtime on a bit-by-bit basis

    Left-shift << (JLS �15.19)

    * bits are shifted to the left based on the value of the right-operand
    * new right hand bits are zero filled
    * equivalent to left-operand times two to the power of the right-operand
    For example, 16 << 5 = 16 * 25 = 512

    Decimal 16 00000000000000000000000000010000

    Left-shift 5 00000000000000000000000000010000
    fill right 0000000000000000000000000001000000000
    discard left 00000000000000000000001000000000

    * the sign-bit is shifted to the left as well, so it can be dropped off or a different sign can replace it

    Right-shift >> (JLS �15.19)

    * bits are shifted to the right based on value of right-operand
    * new left hand bits are filled with the value of the left-operand high-order bit therefore the sign of the left-hand operator is always retained
    * for non-negative integers, a right-shift is equivalent to dividing the left-hand operator by two to the power of the right-hand operator
    For example: 16 >> 2 = 16 / 22 = 4

    Decimal 16 00000000000000000000000000010000

    Right-shift 2 00000000000000000000000000010000
    fill left 00000000000000000000000000000100
    discard right 00000000000000000000000000000100 -> Decimal 4

    Decimal -16 11111111111111111111111111110000

    Right-shift 2 11111111111111111111111111110000
    fill left 1111111111111111111111111111110000
    discard right 11111111111111111111111111111100 -> Decimal -4

    Unsigned right-shift >>> (JLS �15.19)

    * identical to the right-shift operator only the left-bits are zero filled
    * because the left-operand high-order bit is not retained, the sign value can change
    * if the left-hand operand is positive, the result is the same as a right-shift
    * if the left-hand operand is negative, the result is equivalent to the left-hand operand right-shifted by the number indicated by the right-hand operand plus two left-shifted by the inverted value of the right-hand operand
    For example: -16 >>> 2 = (-16 >> 2 ) + ( 2 << ~2 ) = 1,073,741,820

    Decimal 16 00000000000000000000000000010000

    Right-shift 2 00000000000000000000000000010000
    fill left 00000000000000000000000000000100
    discard right 00000000000000000000000000000100 -> Decimal 4

    Decimal -16 11111111111111111111111111110000

    >>> 2 11111111111111111111111111110000
    fill left 0011111111111111111111111111110000
    discard right 00111111111111111111111111111100

     
    reply
      Bookmark Topic Watch Topic
    • New Topic