是否有非分支按位解决方案来确定 "odd one out of 3" 数字,如果两个相等,否则第一个?

Is there a non-branching bitwise solution to determine the "odd one out of 3" number if two are equal, else the first one?

int64_t foo(int64_t a, int64_t b, int64_t c){
    return a == b ? c : a == c ? b : a;
}

是否有实现上述功能的非分支按位破解?

是的,它们是:

return ((!(a-b))*(c^a))^((!(a-c))*(b^a))^a;

if a=b=c: (1*(c^a))^(1*(b^a))^a = a^b^c= a

if a=b != c: then (1*(c^a))^0^a= c^a^a= c

a=c != b

也一样
if a != b != c: (0*(a^c))^(0*(a^b))^a = 0^0^a=a

这种方式是否更快还有待商榷,不妨试试

UB-警告。 如果 a-ba-c 导致上溢或下溢,这是未定义的行为。可以使用无符号变量。