aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shortcut for Shift operator questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shortcut for Shift operator questions" Watch "Shortcut for Shift operator questions" New topic
Author

Shortcut for Shift operator questions

Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
Hello All.

I have started giving mock tests available on the internet. One problem that I see is that I get stuck in the questions involving shift operators. While learning about shift operators I used calculators to get to the right answer. But calculators are not allowed in the final exam. I get nervous when I come across big numbers carrying only 0's and 1's. Is there any short cut method of solving questions involving shift operators? For example: -4 >>> 3. Could someone tell me all the steps in solving this problem?

Thanks & Regards,
Rekha
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Take it easy. If you look in the Java™ Tutorial, you find this:
Certain operators tend to appear more frequently than others; for example, the assignment operator "=" is far more common than the unsigned right shift operator ">>>".
So you needn't expect to find lots of questions about shift operators.

You usually use them on ints mainly because ints are the most popular type of number, but I shall show you how they work on bytes, mainly because it is quicker to write 8 1s than 32!

1: The left-shift operator. << n

This moves the whole thing n bits to the left; any bits which pass the most significant bits disappear into cyber-limbo and any gaps on the right are filled with 0s. So 21 << 2 is
  • 21 = 00010101
  • Two to the left = 010101--
  • Fill in the 0s = 01010100
  • Convert to decimal = 84.
  • 84 / 2 = 4, and 4 = 2^2. There's your 2 back.
  • Left-shift 1 bit is equivalent to multiplying by 2, two bits is multiplying by 4, and three bits is multiplying by 8.
    The normal rules about overflow errors apply; 21 << 3 in 32 bits is 168 but in 8 bits it's -88. -33 << 2 in 32 bits is -132, in 8 bits it's +124.

    2: The right-shift operator >> n
    This moves the whole thing n bits to the right; any bits which pass the least significant bit fall off the edge of the world and it fills in from the left with the same bit that the original most significant bit was. 21 >> 2 is
  • 21 = 00010101
  • 2 to the right gives --000101
  • Fill in with 0 gives 00000101
  • and that's 5
  • 21 / 5 in integer arithmetic is 4, 4 = 2^2, and there's your 2 back.
  • Not 168, but -88 is 10101000.
  • 2 to the right gives --101010
  • Fill in with 1s this time gives 11101010
  • 11101010 is -22.
  • -88 / -22 is 4, and 4 is 2^2.
  • So right-shift by 1 is the same as dividing by 2, two bits to the right is the same as dividing by 4, three bits is dividing by 8, etc. Note that the sign is always maintained and there is no risk of overflow, but if you shift too many places you will always get -1 from negative numbers and 0 from positive numbers.

    Both the right-shift and left-shift operators are usually described as being "signed."

    3: The unsigned right shift >>> n
    This is the most complicated of the three. It moves the bits n places to the right, and any bits which pass the least significant bit float off into the wild blue yonder, but it fills in from the left with 0s. So it always returns a positive answer, whether the original answer was positive or negative. For positive numbers it has exactly the same effect as the ordinary signed right-shift, so I won't draw it
    For a negative number, eg -88 >>> 2, this is what happens
  • -88 is 10101000
  • Two to the right gives --101010
  • Fill in with ZEROES from the left: 00101010
  • 00101010 is 42.

  • Remember that is you multiply 42 by 4 in 8 bits you get -88. I am afraid I can't think of a simple way to calculate the values you get from a >>> operator.

    I hope this is of some help to you And it was a nice interesting question to answer, too.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39409
        
      28
    Try this little programIt works; I have just tried it.

    But for the last it converts to an int. See what happens; it returns what you get from the right-shift as an int.
    Gitesh Ramchandani
    Ranch Hand

    Joined: Feb 28, 2007
    Posts: 274
    left-shift << is equivalent to multiplying the number by 2,4,8...respectively for shifting by 1,2,3....positions


    OR
    Or stating more simply left-shifting a value by n positions, is the same as multiplying it by 2^n


    For example:

    ***********************************************************************
    right-shift >>/>>> is equivalent to dividing the number by 2,4,8...respectively for shifting by 1,2,3....positions


    OR
    Or stating more simply right-shifting a value by n positions, is the same as dividing it by 2^n


    For example:



    and so on...

    But in case of right-shift [>>] for a negative number, the above shortcuts do not apply.

    Regards,
    Gitesh
    [ March 20, 2008: Message edited by: Gitesh Ramchandani ]
    Rekha Anand
    Ranch Hand

    Joined: Feb 23, 2008
    Posts: 36
    Hello All,

    I am sorry I took long to reply. I appreciate from the bottom of my heart the effort you all have put in to help me understand shifting. I tried some questions manually and was able to solve. There is still slight problem with >>> for negative numbers such as -16>>>3, the 32 bits are over-whelming. For instance...

    -16>>>3
    -16 : 11111111 11111111 11111111 11110000
    -16>>>3 : 11111 11111111 11111111 11111110000
    0 Fill: 00011111 11111111 11111111 11111110

    Now that I have done the shifting " HOW DO I CONVERT THIS BIG BINARY NUMBER INTO DECIMAL DURING THE EXAM..?" It will take up a lot of time.

    Thanks & Regards
    Rekha
    Jelle Klap
    Bartender

    Joined: Mar 10, 2008
    Posts: 1773
        
        7

    Well, unless you are taking the SCJP 1.4 exam, you will not need to perform any bitshifting operations.


    Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
    Shivit Agarwal
    Ranch Hand

    Joined: Feb 28, 2008
    Posts: 82
    Simple, use A.P series most of the calculation can be done in my mind if one is good with mathematics in very few seconds.

    say your bits look like ,
    11111111 1111111 11111100

    As per your convinience take From either right to left or vice versa.
    Say from right,

    Count the no. of 1's until zero is encountered.Therefore consecutive 1's is 22. So, the above case A.P series can be written as

    2^23 + 2^22 +.....2^2+ 0 + 0
    ---------------------
    22's 1 (but mind it all are 1's are consecutive)

    just apply the formula for sum= n/2(2*a+(n-1)/d)

    PS: you may have to work little bit more if alternately bits are there but this method is still very fast.
    [ March 23, 2008: Message edited by: Shivit Agarwal ]

    Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
    Ali Khalfan
    Ranch Hand

    Joined: Nov 03, 2007
    Posts: 126
    Jelle has a good point here. is this on the exam or not?
    Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8829
        
        5
    this is a minor topic on the 1.4 exam - it's not on the 1.5 or 1.6 exams at all


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
     
    Don't get me started about those stupid light bulbs.
     
    subject: Shortcut for Shift operator questions