the operator type & is undefined for the argument type(s) int - What gves?

The reference page says that the bitwise AND operator & can be used with variable type int, but when you try it you get an error. Any idea what is going on? Is it Processing, the reference page or me? Processing 3.3.3 on a Mac Thanks

Answers

  • Post the statement giving the error.

  • int num = 0xE6;
     if(num & 0x01 == 0)
     { print("fine");
     }
    

    This works in C

  • edited May 2017 Answer ✓

    maybe like this? I dunno.

    In your version your & might be treated as &&

        int num = 0xE6;
        if ((num & 0x01) == 0)
        { 
          print("fine");
        }
    
  • edited May 2017

    The problem appears to be that Processing has evaluated the 0x01 == 0 first and then tries to do the bitwise & between an int and a boolean. The solution is to use parentheses to clarify the operedr of evaluation like this

    int num = 0xE6;
    if ((num & 0x01) == 0)
    { 
      print("fine");
    }
    
  • edited May 2017

    Like every1 said above. Just an extra info:
    & 1 is used to determine whether a value is even or odd: B-)

    int val = (int) random(10);
    boolean isEven = (val & 1) == 0;
    println(val, isEven);
    exit();
    
  • edited May 2017

    Another curious case in Java: Operators &, | and ^ are overloaded to accept boolean operands:

    if (mouseX > width>>2 & mouseX < 3*width/4) println("Active region"); :)>-

    However, both sides of the bitwise operators are always eagerly evaluated. L-)

    Unlike their logical counterparts && and ||, which are lazy-evaluated and short-circuits the right side if the result can already be determined by just the left side operand. :-B

  • Thanks guys that did it.

  • the actual error message is

    "The operator & is undefined for the argument type(s) int, boolean"

    things are so much easier when people give the exact error message.

Sign In or Register to comment.