位运算实现三元表达式

15213 - Recitation 3 - Datalab

Find an algorithm for computing the expression (cond) ? t : f , which equals t if cond is
1 and f if cond is 0.

int conditional(int cond, int t, int f) {
/* Compute a mask that equals 0x00000000 or
0xFFFFFFFF depending on the value of cond */
int mask = cond - 1;
/* Use the mask to toggle between returning t or returning f */
return mask&f|~mask&t;
}
  没有英汉互译结果
  请尝试网页搜索

原文地址:https://www.cnblogs.com/azureice/p/8410143.html