• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bitwise operator

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am prepared for the exam and loginto http://www.danchisholm.net/july21/mybook/chapter3/exam1.html to do some mock exam.
1. I want to know if I need to learn how to convert to binary for SCJP1.4?
2. This block of code giving the answer of 3,0,3. Please anyone can explain how to get it, I compile and run it gave me the answer
Thnks for your help

class EBH020 {
public static void main (String[] args) {
int a = 1 | 2 ^ 3 & 5;
int b = ((1 | 2) ^ 3) & 5;
int c = 1 | (2 ^ (3 & 5));
System.out.print(a + "," + b + "," + c);
}}

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jamie lee:
class EBH020 {
public static void main (String[] args) {
int a = 1 | 2 ^ 3 & 5;
int b = ((1 | 2) ^ 3) & 5;
int c = 1 | (2 ^ (3 & 5));
System.out.print(a + "," + b + "," + c);
}}



1 | 2 ^ 3 & 5 =>
1 | ( 2 ^ 3 & 5 ) =>
1 | ( 2 ^ ( 3 & 5 ) ) =>
001 | ( 010 ^ ( 011 & 101 ) ) =>
001 | ( 010 ^ 001 ) =>
001 | 011 =>
011 => 3

note : all the binary numbers has 32 digits but 29 are hided by me ...
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will help you with the first one. You will have to do the rest.

First, convert to its bynary form every single number. Integer literals in java are of type int by default. And that means they are 32 bit. However, to simplify the solution I will use 8-bit numbers, because anyway the code snippet does not contain numbers bigger the 8-bit

1 = 00000001
2 = 00000010
3 = 00000011
5 = 00000101

Because the order of precedence of the operators the expresion 3 & 5 is evaluated first.

00000011 (3)
00000101 (5)

Result:
00000001

Thas equal to 1;

Then the XOR is evaluated, thats to say 2^ (3 & 5)

That will be:

00000010 (2)
00000001 (3 & 5)


Result:
00000011

That's equal to 3.

Then the OR evaluated. Thats to say 1 | (2 ^ (3 & 5) )

00000001 (1)
00000011 (2 ^ (3 & 5))

The result:
00000011 (3)

The result is 3.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved this question but I have one doubt is , why we are solving from right to left ... As java solve expression from left to right ...
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The expression indeed is evaluated from left to right, is just the precedence of the operators stablishes that the first operator to evaluated is & the ^ and finally |

Run this snippet and you will undestand:


If I am not wrong the expressions will be evaluated from left to right, but the application of the operators will be according to their precedence.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means that their is a difference between evaluation of the expressions and the application of the operators. Expressions are evaluated from left to right. The operators are applied according to their precedence.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Edwin ,
please tell me , how you are doing this ...
I mean which table you have in your mind for preference or anything ...
please help me ...
thanks a lot .
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please visit this site to get your answer:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html

I hope it helps.

By the way, Are you using any book to study for this certification?

I recommend you to visit:
http://safari.java.net
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic