我已经被这个按位拼图困住了几个小时。我在谷歌上寻找这个问题的一个很好的解决方案,看看其他人是如何解决它的,我遇到了这个解决方案。我想弄清楚它是如何工作的。我唯一不明白的是舍入过程和丢失的位?丢失了哪些位?
我真的很感激有人向我解释这个解决方案。
在此先感谢大家,优胜美地 :)
/*
* ezThreeFourths - multiplies by 3/4 rounding toward 0,
* Should exactly duplicate effect of C expression (x*3/4),
* including overflow behavior.
* Examples: ezThreeFourths(11) = 8
* ezThreeFourths(-9) = -6
* ezThreeFourths(1073741824) = -268435456 (overflow)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 3
*/
int ezThreeFourths(int x) {
/*
*sets y to make up for the lost bits while rounding. Multiplies
*by three by …Run Code Online (Sandbox Code Playgroud)