objective-cgcc

Invalid operands to binary


I have a method to check weather a number is even or odd:

   -(BOOL)numberIsEven:(unsigned int *)x {


  if (x & 1)
 {
  return TRUE;
 }
 else
{
 return FALSE;
  }
}

however whenever I compile it I get the error:

Invalid operands to binary %

So it's compiling into assembly as a modulus function and failing, somehow, however if I use a modulus based function (arguably slower) I get the same error!


Solution

  • x is a pointer. The modulo operator will not work on pointers.

    return (*x & 1);
    

    This dereferences the pointer, then returns the result of the modulo (implictly cast to a BOOL)