aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes bitwise 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 "bitwise operator" Watch "bitwise operator" New topic
Author

bitwise operator

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
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);
}}

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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 ...
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 31, 2004
Posts: 961
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
 
 
subject: bitwise operator