有没有办法仅使用这些二元运算(n
、1
、!
、~
、&
、^
、{1045)获取 |
个 +
67915 } , <<
) 其中 >>
是输入?
示例,
n ---> output
0 ---> 0000
1 ---> 0001
2 ---> 0011
3 ---> 0111
4 ---> 1111
...
最佳答案
你可以这样做:
// Since "-" is not on your list while "+" is, I'll add negative 1
// using `~0`; this assumes that your computer uses 2's complement
// representation of negative numbers.
(1 << n) + ~0;
这个想法是 1 << n
产生 2 的幂:1
, 10
, 100
, 1000
, 等等。添加一个负数产生 2^n-1
,这就是您的模式所代表的内容。
关于c - 使用二元运算计算 n 个 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16189775/