# Question on Shift - left Operator .

Joe Man

Ranch Hand

Posts: 71

posted 14 years ago

Hello All,

This is my first time posting a message.

Can someone explain to me why the answer is 56?

public class Empty{

public static void main(String args[]){

System.out.println(7 << 32003);

}

}

This is my first time posting a message.

Can someone explain to me why the answer is 56?

public class Empty{

public static void main(String args[]){

System.out.println(7 << 32003);

}

}

Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform

Rob Ross

Bartender

Posts: 2205

posted 14 years ago

Do a search on shift operators in this forum and you will find many many threads about this topic.

The short answer is that when the left-hand operand is an int type, only the 5 least-significant bits of the right-hand side operand are used. If the left-hand side operand is a long type, then the least-significant 6 bits of the right-hand side operand are used.

The short answer is that when the left-hand operand is an int type, only the 5 least-significant bits of the right-hand side operand are used. If the left-hand side operand is a long type, then the least-significant 6 bits of the right-hand side operand are used.

Rob

SCJP 1.4

Zac Roberts

Ranch Hand

Posts: 82

posted 14 years ago

This answer is 56 because if you shift by more than your capacity then you have to shift by the modulus. What this means is that (7 << 32003) means we are shifting an int (7) which has a capacity of 32 bits. Well, 32003 is way more than our capacity of only 32 bits. So we have to take 32003 % 3 which gives 3 because of course % returns the remainder of 32003 divided by 3. So what we are really evaluating is (7 << 3) which is 56.

Zac

Zac

Zac Roberts<br />SCJP2

Joe Man

Ranch Hand

Posts: 71

posted 14 years ago
Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform

I think i got the idea:

So the binary string for 32003 is:

111110100000011

Take 5 least-significant bits of the right-hand side operand:

000011 is 3

So 7*(2^3) = 56

My concern is this.. During the exam, if they give me 7<<33003 instead of 7<<32003, how would i be able to figure out the binary string for this large number and try to calculate the answer?

So the binary string for 32003 is:

111110100000011

Take 5 least-significant bits of the right-hand side operand:

000011 is 3

So 7*(2^3) = 56

My concern is this.. During the exam, if they give me 7<<33003 instead of 7<<32003, how would i be able to figure out the binary string for this large number and try to calculate the answer?

Joe Man

Ranch Hand

Posts: 71

posted 14 years ago
Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform

Zac,

I'm not really following you. "So we have to take 32003 % 3 which gives 3 because of course % returns the remainder of 32003 divided by 3."

32003 % 3 is 2 not 3.

Can you please explain? thanks..

I'm not really following you. "So we have to take 32003 % 3 which gives 3 because of course % returns the remainder of 32003 divided by 3."

32003 % 3 is 2 not 3.

Can you please explain? thanks..

Zac Roberts

Ranch Hand

Posts: 82