What does int c = (a+b) >>1
mean in C++?
在C ++中,int c =(a + b)>> 1是什么意思?
6 个解决方案
#1
12
Note, that there can't be any meaningful explanation of what your code means until you explain what a
and b
are.
请注意,在解释a和b之前,对代码的含义没有任何有意义的解释。
Even if a
and b
are of built-in type, beware of the incorrect answers unconditionally claiming that built-in right shift is equivalent to division by 2. The equivalence only holds for non-negative values. The behavior of the >>
operator for negative values is implementation-defined.
即使a和b是内置类型,也要注意错误的答案,无条件声称内置右移相当于除以2.等价仅适用于非负值。 >>运算符对负值的行为是实现定义的。
In other words, without extra information, the only thing that can be said is that the code calculates the "sum" a + b
and "shifts" it right by 1 bit. I used quotes in the last sentence because in case of overloaded operators +
and >>
there's no way to predict what they are doing.
换句话说,没有额外的信息,唯一可以说的是代码计算“和”a + b并将其“右移”1位。我在最后一句中使用了引号,因为在重载运算符+和>>的情况下,没有办法预测它们在做什么。
#2
18
It returns the average of a
and b
, rounded down. So, if a
is 5 and b
is 8, then the result is 6.
它返回a和b的平均值,向下舍入。因此,如果a为5且b为8,则结果为6。
ETA: This method is busted if a
and b
add up to a negative number, like if both are negative, or if integer overflow occurs.
ETA:如果a和b加起来为负数,如果两者都为负数,或者发生整数溢出,则此方法被终止。
#3
6
That depends on the type of c, a and b. If it's int then the above statement is the same as:
这取决于c,a和b的类型。如果是int,则上述语句与以下语句相同:
c = (a+b)/2;
>>
means shift right one bit.
>>意味着向右移一位。
#4
2
It means to add A to B, then bit-shift the result by one bit to the right. Bit-shifting a positive integer generally has the effect of multiplying or dividing by 2^n where n is the number of bits being shifted. So, this is roughly equivalent to (a+b)/2 in integer math (which has no remainders or fractional parts).
这意味着将A添加到B,然后将结果向右移位一位。对正整数进行位移通常具有乘以或除以2 ^ n的效果,其中n是被移位的位数。因此,这大致相当于整数数学中的(a + b)/ 2(没有余数或小数部分)。
#5
1
It means that you add a
and b
, then shift the result one bit to the right.
这意味着您添加a和b,然后将结果向右移动一位。
It's the same as:
它与以下相同:
int c = (a + b) / 2;
#6
1
As mentioned above, it's an average function utilizing the bit-shift operator in c++ (with some potential pitfalls in it) - by the existence of this question, the readability of this code is quite bad. Do your fellow programmer a favor and think about readability when you write code
如上所述,它是利用c ++中的位移运算符的平均函数(其中存在一些潜在的缺陷) - 由于存在这个问题,这段代码的可读性非常糟糕。在编写代码时,请帮助程序员,并考虑可读性
#1
12
Note, that there can't be any meaningful explanation of what your code means until you explain what a
and b
are.
请注意,在解释a和b之前,对代码的含义没有任何有意义的解释。
Even if a
and b
are of built-in type, beware of the incorrect answers unconditionally claiming that built-in right shift is equivalent to division by 2. The equivalence only holds for non-negative values. The behavior of the >>
operator for negative values is implementation-defined.
即使a和b是内置类型,也要注意错误的答案,无条件声称内置右移相当于除以2.等价仅适用于非负值。 >>运算符对负值的行为是实现定义的。
In other words, without extra information, the only thing that can be said is that the code calculates the "sum" a + b
and "shifts" it right by 1 bit. I used quotes in the last sentence because in case of overloaded operators +
and >>
there's no way to predict what they are doing.
换句话说,没有额外的信息,唯一可以说的是代码计算“和”a + b并将其“右移”1位。我在最后一句中使用了引号,因为在重载运算符+和>>的情况下,没有办法预测它们在做什么。
#2
18
It returns the average of a
and b
, rounded down. So, if a
is 5 and b
is 8, then the result is 6.
它返回a和b的平均值,向下舍入。因此,如果a为5且b为8,则结果为6。
ETA: This method is busted if a
and b
add up to a negative number, like if both are negative, or if integer overflow occurs.
ETA:如果a和b加起来为负数,如果两者都为负数,或者发生整数溢出,则此方法被终止。
#3
6
That depends on the type of c, a and b. If it's int then the above statement is the same as:
这取决于c,a和b的类型。如果是int,则上述语句与以下语句相同:
c = (a+b)/2;
>>
means shift right one bit.
>>意味着向右移一位。
#4
2
It means to add A to B, then bit-shift the result by one bit to the right. Bit-shifting a positive integer generally has the effect of multiplying or dividing by 2^n where n is the number of bits being shifted. So, this is roughly equivalent to (a+b)/2 in integer math (which has no remainders or fractional parts).
这意味着将A添加到B,然后将结果向右移位一位。对正整数进行位移通常具有乘以或除以2 ^ n的效果,其中n是被移位的位数。因此,这大致相当于整数数学中的(a + b)/ 2(没有余数或小数部分)。
#5
1
It means that you add a
and b
, then shift the result one bit to the right.
这意味着您添加a和b,然后将结果向右移动一位。
It's the same as:
它与以下相同:
int c = (a + b) / 2;
#6
1
As mentioned above, it's an average function utilizing the bit-shift operator in c++ (with some potential pitfalls in it) - by the existence of this question, the readability of this code is quite bad. Do your fellow programmer a favor and think about readability when you write code
如上所述,它是利用c ++中的位移运算符的平均函数(其中存在一些潜在的缺陷) - 由于存在这个问题,这段代码的可读性非常糟糕。在编写代码时,请帮助程序员,并考虑可读性