Reputation: 91
i'm converting a c++ header to java and the following does not work. opstring is a string and alumacOP is a string array. opindex is an int.
opstring = alumacOP[opindex][(op >> 11) & 3 == 3]; //code does not work.
this does not work for some reason. it's not the algorithm, but eclipse saying that '&' is not defined for types boolean and int.
so my question is: Why does logical AND reference only Boolean values in java? because this works:
boolean boola,boolb;
if(boola & boolb)
return;
but not this (eclipse still throws an error:
int x = 9, y = 8;
for(int i = 0; i < 100; i++)
if(x & y = i)
return;
Upvotes: 0
Views: 1945
Reputation: 16168
It depends on operator precedence.
opstring = alumacOP[opindex][(((op >> 11) & 3) == 3)?1:0];
is what you want. (note the parenthesis around &, and the ?: to convert bool to int.
Upvotes: 3
Reputation: 264
The & you see is the bitwise AND, not the logical AND.
And the reason you see that it throws an error is because your if statement conditional is an assignment, not a check.
You need to use 2 equal signs (==) such as it says if (x & y == i)
Upvotes: 1
Reputation: 23265
In java you can do &
on two boolean
s or two int
s, but you can't do it on one of each. Also watch for your order of operations, and the difference between =
and ==
.
Upvotes: 0