7c00h
7c00h

Reputation: 91

java - operator & not defined for types (boolean, int)

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

Answers (3)

nothrow
nothrow

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

user829323
user829323

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

Keith Randall
Keith Randall

Reputation: 23265

In java you can do & on two booleans or two ints, but you can't do it on one of each. Also watch for your order of operations, and the difference between = and ==.

Upvotes: 0

Related Questions