• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

operator

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Weerawit Maneepongsawat
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank valentin but i confuse about operator >> , >>>, /, *
which operator must done frist
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changed it. Sorry.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin.
 
Weerawit Maneepongsawat
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank valentin
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
The only cure for that is hours of television radiation. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic