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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Shift operators

Ranch Hand
Posts: 48
ok it was a help going through the discussion on shift operators started by Kavitha. I'm planning to take the exam on 14 oct and totally freaked out. About shift operators. From Mughal's book i've learnt that << is like multiplying by 2 and >> is like dividing the left operand by 2 for each shift. So now here i learnt that we use % operator on the right operand. Can someone plz list down some basic rules about shift operators. I really have a problem with this section. Thanks. And its great to be part of this forum. :roll:

Greenhorn
Posts: 25
Hi Saniya,
I was stumped by shift operators for a while, I've gotta say unsigned shift operator is still a bit of a mystery for me, but for << and >> here goes with my explanation.
a<<b shifts bits in a to the left b number of spaces. For example 3<<2, converting 3 to binary we get 11. 11 shifted left two places is 1100 (Shifting left pads with zeros). 1100 in decimal is 12. The same works for right shifting , except for there's no need to pad with zeros.
The modulous operator (%) returns the remainder after dividing a with b (c=a%b).
Hope this helps.

Originally posted by saniya:
ok it was a help going through the discussion on shift operators started by Kavitha. I'm planning to take the exam on 14 oct and totally freaked out. About shift operators. From Mughal's book i've learnt that << is like multiplying by 2 and >> is like dividing the left operand by 2 for each shift. So now here i learnt that we use % operator on the right operand. Can someone plz list down some basic rules about shift operators. I really have a problem with this section. Thanks. And its great to be part of this forum. :roll:

Saniya Ansari
Ranch Hand
Posts: 48
thanks for ur reply kem actually i referred to this discussion: https://coderanch.com/t/238965/java-programmer-SCJP/certification/Shifting
"Because Java treats i>>j as i>>(j%32) , and
i>>>j as i>>>(j%32), if i is an integer." which was posted by Ron Newman so i'm wondering whats the deal here..

Ranch Hand
Posts: 51
for ints, all that means is that if java gets i<<j and j= 45 then it becomes i<<45%32 which is i<<13. if i is a long then it i think uses 64 as the modulo. this is to ensure that the shift is always in int or long range:
"If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive."
i did this:
int start = 100;
int littleShift = 13;
int bigShift=45;

System.out.println("little shift = " + (start<<littleShift));
System.out.println("big shift = " + (start<<bigShift));
and got
little shift = 819200
big shift = 819200
so i guess this is the case. that is my reading of "15.19 Shift Operators" and the shift-distance modulo stuff - only a greenhorn, so welcome any clarifications.
[ October 01, 2002: Message edited by: peter greaves ]

Ranch Hand
Posts: 39
Can I make this a little more complicated?
Can someone tell me how to calculate the result of a negative shift?
For example:

The results are:
0
0
838860800

Ranch Hand
Posts: 375
Hi Terrence,
From the JLS 15.19 Shift Operators -
..........If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance.
..........If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance.
What the above means is that when we shift something -9 times we are actually shifting the value by 23 times . Here's how -
-9 = 1111 1111 1111 1111 1111 1111 1111 0111
From the JLS quote above we need to consider only the five lowest-order bits as our shift distance which is
10111 == 23
So 100 >> -9 == 100 >> 23
You can figure out the rest now.
HTH
Ashish H.

Ranch Hand
Posts: 787
Hi Guys:
After reading this discussion, I thought there must be a simple set of rules for shift operations as Saniya is looking for in the following quote:

ok it was a help going through the discussion on shift operators started by Kavitha. I'm planning to take the exam on 14 oct and totally freaked out. About shift operators. From Mughal's book i've learnt that << is like multiplying by 2 and >> is like dividing the left operand by 2 for each shift. So now here i learnt that we use % operator on the right operand. Can someone plz list down some basic rules about shift operators. I really have a problem with this section. Thanks. And its great to be part of this forum.

Here are rules as I see them:
Rules for Shift Operators
A op B
Assuming A is any integral value except long. For long A, replace 32 with 64 in the following:
1. B = B%32
2. If B is negative and not -32 : B=B+32
3. For signed right shift operator, divide A successively by 2, B number of times. Round down the final result if A is positive. Round up the final result if A is negative.
4. For signed left shift operator, multiply A succesively by 2, B number of times. If (32-B)th bit of original A value is 1, make the result –ve if it is not already negative. If the final result is more than Integer.MAX_VALUE, make final result 0.
5. For unsigned right shift operator with postive A, same rules as number 3 above.
6. For unsigned right shift operator with negative A, do it conventional way:
a. find the two's compliment of negative A
b. right shift by B places, inserting 0 on the left
c. find the decimal of above.
Examples:
int A = 200, B = 2
A >> B;
1. B=B%32 => B=2
2. B=2
3. result=50
Try more for yourself. Let me know if you have any questions.
Thanks
Barkat

Saniya Ansari
Ranch Hand
Posts: 48
Thanks Barkat! that was pretty helpful..i'll surely ask if any problems !

Author & Gold Digger
Posts: 7617
6
saniya,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:

For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.

 It would give a normal human mental abilities to rival mine. To think it is just a tiny ad: Thread Boost feature https://coderanch.com/t/674455/Thread-Boost-feature