t********e 发帖数: 25 | 1 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 | t********e 发帖数: 25 | 2 I am thinking:
x = x >> 8 | x >> 7 | x >> 6 | x >> 5 | x >> 4 | x>>3 | ... | x>>1
return x&y + ~x&z
anyone has better format?? | b****j 发帖数: 78 | 3 !x + ~0 & y | !!x + ~0 & z
【在 t********e 的大作中提到】 : 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
| g*******y 发帖数: 1930 | 4 how about
-!x & (y^z) ^ y
only using 5 operations (or 6 if you count "-"(negate) as two: -x = ~x + 1) | b****j 发帖数: 78 | 5 nice!
【在 g*******y 的大作中提到】 : how about : -!x & (y^z) ^ y : only using 5 operations (or 6 if you count "-"(negate) as two: -x = ~x + 1)
| a********a 发帖数: 219 | 6 能解释以下么?什么叫根据x状态return y, z? x多少的时候return y?多少return z?
【在 t********e 的大作中提到】 : 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
|
|