simple java question

F

Fahad

b=((1|2)^3)&5
acording to java cert book ans b =0
according to calculator b=1
does any one know why
ur answers will be highly apreciated
 
G

Gary Labowitz

Fahad said:
b=((1|2)^3)&5
acording to java cert book ans b =0
according to calculator b=1
does any one know why
ur answers will be highly apreciated

To see where you might be going wrong, take it one step at a time.
What results did you get with "calculator" for
1 | 2
(1|2)^3
((1|2)^3) & 5

I get 0, of course.
 
J

Joona I Palaste

Fahad said:
b=((1|2)^3)&5
acording to java cert book ans b =0
according to calculator b=1
does any one know why
ur answers will be highly apreciated

What kind of calculator are you using? Let's do it by hand.
1|2 == 3
3^3 == 0
0&5 == 0

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"As a boy, I often dreamed of being a baseball, but now we must go forward, not
backward, upward, not forward, and always whirling, whirling towards freedom!"
- Kang
 
P

Peter Kirk

Fahad said:
b=((1|2)^3)&5
acording to java cert book ans b =0
according to calculator b=1
does any one know why
ur answers will be highly apreciated

I am wondering if the ^ symbol is confusing here?

In java it is "exclusive or". Maybe your calculator keyboard has a symbol
which looks like this and means "exponent/to-the-power-of" ?

(just a guess).

Peter
 
A

Alex Hunsley

Fahad said:
b=((1|2)^3)&5
acording to java cert book ans b =0
according to calculator b=1
does any one know why
ur answers will be highly apreciated

Try using full stops, capital letters, full words and not hideous
abbreviations like "ur".
 
P

Peter Ashford

Peter said:
I am wondering if the ^ symbol is confusing here?

In java it is "exclusive or". Maybe your calculator keyboard has a symbol
which looks like this and means "exponent/to-the-power-of" ?

(just a guess).

Peter

That was my guess, and to verify I ran the calculation through my
calculator using ^ as "to the power of" and the result was 1, so I think
you've got it right.
 
J

Joona I Palaste

That was my guess, and to verify I ran the calculation through my
calculator using ^ as "to the power of" and the result was 1, so I think
you've got it right.

Let's see now, one step at a time.
1|2 == 3 (binary 11)
3 to the power of 3 == 27 (binary 11011)
27&5 == 1 (in binary: 11011 & 101 == 1)

So this is most likely the right explanation.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top