由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ 一小题
相关主题
某银行的笔试题C++ Q29: extern and const together
一个loop里放两个operation,和两个Loop里各放一个operation(回忆了几道题)有人做过 select2perfrom 的test吗 ?
再来一道简单的bit运算题C++ question
两整数相除问题搞不清C++里的constant expression
请教一个C内存泄露问题请教个问题
[合集] Amazon onsite 面经一道onsite面试题
LC: happy number怎么保证一定停机或循环?C++ Q62: loop in a linked list (Bloomberg)
Q in C/C++新手请教:C++ decrement loop (转载)
相关话题的讨论汇总
话题: int话题: constants话题: using话题: return话题: only
进入JobHunting版参与讨论
1 (共1页)
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

1 (共1页)
进入JobHunting版参与讨论
相关主题
新手请教:C++ decrement loop (转载)请教一个C内存泄露问题
请教一道题[合集] Amazon onsite 面经
问个C++的基础问题LC: happy number怎么保证一定停机或循环?
请教:find Kth prime numberQ in C/C++
某银行的笔试题C++ Q29: extern and const together
一个loop里放两个operation,和两个Loop里各放一个operation(回忆了几道题)有人做过 select2perfrom 的test吗 ?
再来一道简单的bit运算题C++ question
两整数相除问题搞不清C++里的constant expression
相关话题的讨论汇总
话题: int话题: constants话题: using话题: return话题: only