Chinaunix首页 | 论坛 | 博客
  • 博客访问: 646353
  • 博文数量: 263
  • 博客积分: 9025
  • 博客等级: 中将
  • 技术积分: 2567
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-01 17:42
文章分类

全部博文(263)

文章存档

2012年(4)

2011年(64)

2010年(47)

2009年(44)

2008年(99)

2007年(5)

我的朋友

分类: C/C++

2011-04-18 14:33:13

If you're familiar with the ? operator x ? y : z
you want to implement that in a function: int cond(int x, int y, int z); using only ~, !, ^, &, +, |, <<, >> no if statements, or loops or anything else, just those operators, and the function should correctly return y or z based on the value of x. You may use constants, but only 8 bit constants. You can cast all you want. You're not supposed to use extra variables, but in the end, it won't really matter, using vars just makes things cleaner. You should be able to reduce your solution to a single line in the end though that requires no extra vars.

int cond(int x, int y, int z)
{
 return (((!x)+(~0))&(y^z))^z;
}
阅读(2131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~