What does int c = (a+b) >>1
mean in C++?
In more detail, 1 << j uses shifting of 1 to generate a bit mask in which only the j -th bit is set. The & operator then masks out the j -bit of counter ; if the result is not zero (which means that the j -th bit of counter was set), the condition is satisfied.
The symbol of right shift operator is >> . For its operation, it requires two operands. It shifts each bit in its left operand to the right. The number following the operator decides the number of places the bits are shifted (i.e. the right operand).
1 << 0 is 1 shifted to the left by 0 positions, which is just 1.
>> is a right shift operator. << is a left shift operator. s >> 4 takes the value in 's' and shifts it right 4 bits. example: 1.
It returns the average of a
and b
, rounded down. So, if a
is 5 and b
is 8, then the result is 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With