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: 481
• Number of slices to send:
Optional 'thank-you' note:
--------------------------------------------------------------------------------
Here i don't understand for operator below.Please give me logic to calculate .Are there any formula for the below one .

-5 >> 2 = -2
--------------------

Regards,
Karu

Ranch Hand
Posts: 1277
• Number of slices to send:
Optional 'thank-you' note:
hi karthik.

the >> operator operating on negative operands is really boring
hope the exam doesnt ask such things
i had worked on these questions before, but not able to recall now

Ranch Hand
Posts: 70
• Number of slices to send:
Optional 'thank-you' note:
HI ,
the shift operators are not in syllabus of SCJP 1.5

Karu Raj
Ranch Hand
Posts: 481
• Number of slices to send:
Optional 'thank-you' note:
but i am preparing for SCJP1.4

Sheriff
Posts: 14691
16
• Number of slices to send:
Optional 'thank-you' note:
Please try to not post the same question to different forums.
Thanks

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
Hai Karthik

First convert -5 into bit pattern using 2's complement and shift 2 bits to right as follows: Remember left most bit represents sign of a bit, if it is 1 it is negative otherwise it is postive.

conversion of -5 into bit pattern:

first take +5 and convert into bit pattern(for the convenience I am giving 4 bits only, you can extend upto 8,16 or 32 as required)

5= 0101; Now 2's complement for conversion to -5, which is as follows:
first flip the bits 0101 as 1010 and add 1 to it, resultant bits are as
1010
1
____
1011
Hence -5 is equivalent to 1011

Now apply -5>>2 = 1110, which is nothing but -2

To test whether it is -2 or not, you can test with 2's complement again as exaplained above, then you will get +2(test it).

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
-5>>2

There is a formula to calculate easily
if n>>p
then the result is n/(p pow 2) if n is positive.
then the result is -((n/p pow 2)+ d) if n is negative. put d=1 always,if there is any remainder in division.
So the result of ur question is:
-((5/4)+1)=-2
[ December 28, 2005: Message edited by: santosh kothapalli ]

Karu Raj
Ranch Hand
Posts: 481
• Number of slices to send:
Optional 'thank-you' note:
thanks for the great explanation

how about for 5>>>64

do let me know

santosh kothapalli
Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Karthik Rajashekaran:
thanks for the great explanation

how about for 5>>>64

do let me know

if the number is greater than 32 divide it by 3
in ur case 5>>>64 is equivalent to 5>>>0
which results to 5.
Hope i cleared ur doubt

cheers,
Santosh.

 Don't MAKE me come back there with this tiny ad: a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com
reply
Similar Threads