mercredi 5 février 2020

One and only one zero comparison with the least operations

This might be a silly question, but in some function I have two int s, and need to compare them to find if exactly one of them is ==0... Both of the ways I came up with require 5 logical operations, and I can't think of anything shorter... Is there a shorter way?

What I came up with is:

int x, y;

if (((x==0) || (y==0)) && (x != y)){} // most obvious option, or:

if (((x^y) == x) ^ ((x^y) == y)){} // looks better, but still 5 ops

Aucun commentaire:

Enregistrer un commentaire