一个面试题,不使用函数,宏,只使用纯运算符构成表达式,判断一个整数的符号
例如:short x;
有几种方法使得一个表达式 K, 让x经过一定的运算,结果:当x>0 时K=1;x<0时K=-1;x=0时K=0
K中不能有函数,宏,只能含有x和基本运算符
高手有几种办法?
[解决办法]
if语句不就完了吗
[解决办法]
if(k < 0)
负数
[解决办法]
x = (k==0) ? 0 : k / (k > 0 ? k : -k);
这行不?
[解决办法]
除了楼上的方法,再给你个方法:
x>0?k=1:(x==0?k=0:k=-1);
[解决办法]
k = (x > 0) - (x < 0);
[解决办法]
抱歉,没看到不能用if和三眼操作
那只能想到用位操作了
a^b的最高位 // 判断符号是否相同
a-b的最高位 // 符号相同时,判断大小
[解决办法]
k = (n<0)*-1+(n>0);
[解决办法]
用位操作不难完成,楼主可以参考下《位操作基础篇之位操作全面总结》
http://blog.csdn.net/morewindows/article/details/7354571
[解决办法]
k = !!(x > 0) - !!(x < 0);
[解决办法]
?:也是运算符呀。
一般的对于整数,k=cond?A:B可以写成 k=(cond)*A + (!cond)*B (认为逻辑真值用1表示)
所以6楼的用?:写的表达式 x>0?1:(x==0?0:-1) 可以改写为:
(x>0)*1 + ( !(x>0) ) * ( (x==0)*0 + (!(x==0))*-1 )
带有 *1 的项不变,把 *-1 改为减法,删掉带有 *0 的子项
(x>0) - ( !(x>0) ) * ( !(x==0) )
合并逻辑式 !A&!B=!(A+B)
(x>0) - !( x>0 + x==0 )
注意第二项!(x>=0),就是x<0,于是化简为
(x>0) - (x<0)
得到8楼的答案。
[解决办法]
(-x) &x=1; -((-x) &x))=-1;x-x=0;
1) 整数 k= x> (x-x) ? (-x) &x : x==x-x ? x-x ? -((-x) &x))
2) 实数,整数 k= x> (x-x) ? x/x :( x==x-x ? x-x :-x/x)
[解决办法]
三目和比较都不允许的话可以这么搞
return x>>31
[解决办法]
((unsigned int)(-x))>>31
[解决办法]
1、(bool)x
[解决办法]
(x >> 16)
2、(x > 0) + (x >> 15)
------解决方案--------------------
抄的:
Compute the sign of an integer
int v; // we want to find the sign of v
int sign; // the result goes here
// CHAR_BIT is the number of bits per byte (normally 8).
sign = -(v < 0); // if v < 0 then -1, else 0.
// or, to avoid branching on CPUs with flag registers (IA32):
sign = -(int)((unsigned int)((int)v) >> (sizeof(int) * CHAR_BIT - 1));
// or, for one less instruction (but not portable):
sign = v >> (sizeof(int) * CHAR_BIT - 1);
The last expression above evaluates to sign = v >> 31 for 32-bit integers. This is one operation faster than the obvious way, sign = -(v < 0). This trick works because when signed integers are shifted right, the value of the far left bit is copied to the other bits. The far left bit is 1 when the value is negative and 0 otherwise; all 1 bits gives -1. Unfortunately, this behavior is architecture-specific.
Alternatively, if you prefer the result be either -1 or +1, then use:
sign = +1
[解决办法]
(v >> (sizeof(int) * CHAR_BIT - 1)); // if v < 0 then -1, else +1
On the other hand, if you prefer the result be either -1, 0, or +1, then use:
sign = (v != 0)
[解决办法]
-(int)((unsigned int)((int)v) >> (sizeof(int) * CHAR_BIT - 1));
// Or, for more speed but less portability:
sign = (v != 0)
[解决办法]
(v >> (sizeof(int) * CHAR_BIT - 1)); // -1, 0, or +1
// Or, for portability, brevity, and (perhaps) speed:
sign = (v > 0) - (v < 0); // -1, 0, or +1
If instead you want to know if something is non-negative, resulting in +1 or else 0, then use:
sign = 1 ^ ((unsigned int)v >> (sizeof(int) * CHAR_BIT - 1)); // if v < 0 then 0, else 1
Caveat: On March 7, 2003, Angus Duggan pointed out that the 1989 ANSI C specification leaves the result of signed right-shift implementation-defined, so on some systems this hack might not work. For greater portability, Toby Speight suggested on September 28, 2005 that CHAR_BIT be used here and throughout rather than assuming bytes were 8 bits long. Angus recommended the more portable versions above, involving casting on March 4, 2006. Rohit Garg suggested the version for non-negative integers on September 12, 2009.