aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator" Watch "operator" New topic
Author

operator

Weerawit Maneepongsawat
Ranch Hand

Joined: Apr 11, 2002
Posts: 203
pls help what the result??
public class Question48{
public static void main(String[] args) {
int i = 4*6-3/2<<2*5>>>1%2-4^3;
System.out.println(i);
}
}
why pls explain abount this i'm so scare


Weerawit<br />SCJP 1.4, SCWCD 1.2, SCWCD 1.4, SCBCD 1.3, SCBCD 5.0, SCMAD 1.0, SCJA 1.0, SCDJWS, SCJD, SCEA, SCDJWS 1.5
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You won't get any questions like this in the real exam, that's why I wrote at the top of my mock exam that it is much more difficult than the real exam and that you should not take it until you feel really comfortable. I have created this mock exam for people who would like to delve a little deeper into the details of the Java language, but it is by no means required to have that level of knowledge to pass the SCJP exam.
Anyway, the question has to do with operator precedence, the whole evaluation details are provided in the answer when you click on the Submit button at the bottom of the page.
By smartly placing the paranthesis at the right spots we get the following expression
((((4*6)-(3/2))<<(2*5))>>>((1%2)-4))^3
that evaluates to 3 (answer D)
[ March 07, 2003: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Weerawit Maneepongsawat
Ranch Hand

Joined: Apr 11, 2002
Posts: 203
thank valentin but i confuse about operator >> , >>>, /, *
which operator must done frist
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
I have a similar question but not as complex.
In a shift operation, given for example:
a >> b or a << b
Can 'b' have a negative value or rather would it make any sense for it to be negative? Would such questions come out in the exam?
I've figured out the behavior of a negative b for right shift operators but couldnt understand the behavior for left shift operators.


SCJP 1.4<br />SCWCD 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
weerawit,
please see the following operator precedence table: http://www.mindprod.com/jcheat.html (at the bottom)
dnz znd,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Changed it. Sorry.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
As far a shifting is concerned, b will always be taken modulus 32 (or 64 if a is of type long). Thus, the rules of the % (modulus) operator apply to the negative b.
Please check out the discussion Shift with -ve operand???.
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Thanks Valentin.
Weerawit Maneepongsawat
Ranch Hand

Joined: Apr 11, 2002
Posts: 203
thank valentin
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Arithmetic operators have high precedence followed by shift and last bitwise.
So in this case
int ii = 4*6-3/2<<2*5>>>1%2-4^3;
ii = 24-1<<10>>>1-4^3;
ii = 23<<10>>>(-3)^3;
ii = 23<<10>>>29^3;
ii = 0^3;
ii = 3;
The only thing is when the second operand for shift is -ve then convert the negative number to bits and take the lower 5 bits if the first operand is int or take 6 lower bits if the first operator is long. Other wise just add 32 to the number(in this case 32 -3 =29.)
Thats it...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator