• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

>> , >>> operator

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Javaguru,
I don't understand >>,>>> operator at all. Can any one explain in easy manor.
Also can any one suggest me the site where I can find the latest objective for the programming certification. So if they have included any new stuff , I can be prepare for that.
Thanks in Advance
Ketu
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the objectives for the Java 2 Cert Exam.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> means right shift
>>> means unsigned right shift
>> when right shift the digits being inserted is most significant bit.
For example , let us take number 7 and -7.
1. 7 in binary is 0000 0111 . Other zeros are removed for clarity.
Now say 7 >> 2 . Since Most Signigicant Bit ( MSB) is 0 here so result came as 0000 0001.
Convert this number to decimal , and that is 1.
So 7 >> 2 is 1 in decimal.
Now take -7 >> 2.
Steps
step 1.
Convert 7 to binary.
that is 0000 0111.
step 2.
Then get 1's complement = 1111 1000 (just flip the digits)
b. add 1 to this number+ 1
------------
we got = 1111 1001
------------
Now shift it right 2 places.
Since we have MSB as 1 so 1 will be inserted into this pattern.
Result wil be 1111 1111
So -7 >> 2 came up as 1111 1111.
But how to read this patteren.
Follow the same approach .
That is ( 1111 1111) = Negative(1111 1111) * -1
So lets find out negative of 1111 1111 and substitute in above equation.
step 1 . Flip all digits --- > 0000 0000
step 2 . Add 1 --- > + 1
-----------
Result 0000 0001
-----------
The decimal of 0000 0001 is 1.
Now put thjis value in the equation
( 1111 1111) = Negative(1111 1111) * -1
( 1111 1111) = 1 * -1
( 1111 1111) = -1
Thus the result"
-7 >> 2 = -1.
If it is -7 >>> 2 Then instead of inserting 1 we insert zeros so the result would be
-7 binary is 1111 1001.
Shift two places right inserting 0's so we got
0011 1110.
convert this to decimal we got
62.
Hope this clears your confusion.
Sorry I am not good at explaining. If it heps you then good.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks JM, I understood the >> operator but I am little confuse about >>> operator & the negative part. Could you please explain that.
Thanks
Ketu
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi JM,
Very good explanation. All the ways to do >> and >>> are correct. But in one place I guess the answer is wrong.
Quate:
--------------------------------------------------
we got = 1111 1001
Now shift it right 2 places.
Since we have MSB as 1 so 1 will be inserted into this pattern.
Result wil be 1111 1111
So -7 >> 2 came up as 1111 1111.
-----------------------------------------------------------
By doing >> of 1111 1001 with 2 places we will get
1111 1001 >> 2 = 1111 1110
Now we will convert it into a decimal digit. Since it's MSB is 1 the number is a negative number.
2's complement = 0000 0001 + 1
= (0000 0010) = -2
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still having problems solving some bitwise operations such as:
2>>>-1
-2>>>33
2>>33
2>>>33
2<<33
-2<<33
(1) what happens when we shift by a negative number?
(2) what happens when we shift more than 32 bits in both signed and unsigned cases?
My text book 'All-In-One Java 2 Exam Guide' by Boone does not go into detail about this.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may help a little
http://www.jchq.net/tutorial/BitShift.htm
Marcus
 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marcus... that cleared up some questions I had.
But if we look at Marcus Mock Exam No. 1 ... question 18, I don't see how answer '3' is true. Also, I haven't seen an example anywhere which steps through a negative shift operation answers '1' or '2' below.
The answer is 1,2,3.
18.
Which of the following statements are true?
(1) System.out.println(2>>>-1); will output a result larger than 10
(2) System.out.println(2>>>-1); will output a positive number
(3) 1>>2; will output the number 1
(4)System.out.println(1<<<2);will output the number 4
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think answers 1,2 r orrect, but answer 3 is not
if answer 3 is valid , then we got to write as
2>>1 instead of 1>>2, bcos 1>>2 gives 0
 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you about number 3 not being true.
1>>2
(1) We have integer 1 in bits:
0000 0001
(2) Shift 2 bits to the right:
0000 0000
(3) Bits fall off the end and are lost.
So to me, the result should be 0. Are we missing something or is the answer wrong?
Also, what is the reasoning for answers 1 and 2 being true.
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic