This week's giveaway is in the OCPJP forum.We're giving away four copies of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) and have Paul Anilprem on-line!See this thread for details.
Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
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:
• Tim Cooke
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Scott Selikoff
Bartenders:
• Piet Souris
• Jj Roberts
• fred rosenberger

# left shift by -2!

Ranch Hand
Posts: 1873
• Number of slices to send:
Optional 'thank-you' note:
Hi,
following code gives o/p of -2147483648. why?
int i=2;
int j=-2;
S.o.p(i<<=j);
regards
maulin.

Bartender
Posts: 2205
• Number of slices to send:
Optional 'thank-you' note:
Do a search in this forum for "bit shit." There is a ton of threads on that subject.
Rob

Ranch Hand
Posts: 1873
• Number of slices to send:
Optional 'thank-you' note:
Hi Rob,
none of them explains "negative" shifting i guess (i'm not sure tho)....if any thread explains that i'd love to go thru that...
i have a theory why it works the way it is working. here it is,
when we try to shift a number -ve times then it takes up the -ve presentation of the right operand and starts shifting it to right (unsigned shift) till it gets +3. then it stops and sees how many times it needed to shift to get +3. say it is N. then it shifts the left operand left N times.
if i have,
int i=5;
int j=-3;
5 = 00000000 00000000 00000000 00000101
-3 = 11111111 11111111 11111111 11111101
if we wonna do i<<= j then it would shift i left N = 29 times as it requires to shift -3 to the right 29times to get +3.
as for i>>=j it will obviously result into ZERO.
regards
maulin.

Rob Ross
Bartender
Posts: 2205
• Number of slices to send:
Optional 'thank-you' note:
Well I will give you credit for theorizing ;p
But that's not quite what happens.
If the lefthand operand is of type long (64 bits) then only the last 6 bits of the right hand side are used. If the lefhand operand is an int (32 bits) then only the last 5 bits of the right hand operator are used.
2 << -2
is
00000000000000000000000000000010 << 1111111111111111111111111111110
But only the last 5 bits of the right hand side are used:
00000000000000000000000000000010 <<
11110
which is equivalent to 2 << 30 ...that last little 1 bit all by itself on the right side moves over 30 places until...
100000000000000000000000000000000
And that's the same value as Integer.MIN_VALUE, which as you discovered is -2147483648.
Rob

Ranch Hand
Posts: 1873
• Number of slices to send:
Optional 'thank-you' note:
yeah..Ros...
i could come up with equally +ve shift as u have 2<<30 after shown that only 5 LSB are used and in long only 6 LSB are used. actually, i didnt give thought to long
the reason i came up with such long theory to follow is, 'coz i started out with the thing in mind that when we do shifting left we "multiply" the number and when we do right shift we divide the no. (by 2 in each case). so, the logic i shd 've shd make some sense. i thought when we have right operand +ve. we can say that the left operand will be shifted as many times as i can shift my right operand to right till i get zero.
so, if i 've 4<<2 it means, 4 is multiplied by 2^2 (=0100) and so 4 must be shifted twice as,
0100 Shift right-----> 0010 Shift Right----> 0001 (can't go beyond as actually we 'll 've only integral part after division each time and so LSB in last stage is actually 0 (complice to my logic for -ve right operand but there i've requirement of +3 here and it is +1).
so, i reasoned in the manner i did. yeah but i agree that was little complex to follow first time. and i forgot to consider longs in it...
its easier to remember the way u phrased the theory in a single sentence

regards
maulin

Ranch Hand
Posts: 1492
• Number of slices to send:
Optional 'thank-you' note:
Hi Maulin,
You must not have performed the correct search. I gave a great answer in the following post:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=011652
Regards,
Manfred.

Ranch Hand
Posts: 1873
• Number of slices to send:
Optional 'thank-you' note:
hi manfred,
thanks for the link. it fits into my mind now that how -ve shifts works.
sorry for not performing extending search.
regards
maulin.

 pie. tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton