Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (a+b) >>1 mean?

Tags:

c++

operators

What does int c = (a+b) >>1 mean in C++?

like image 268
Zebs Avatar asked Sep 23 '10 16:09

Zebs


People also ask

What is the meaning of 1 << J 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.

What does >> represent in C?

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).

What does 1 << 0 do in C?

1 << 0 is 1 shifted to the left by 0 positions, which is just 1.

What does >> mean in CPP?

>> 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.


1 Answers

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.

like image 124
Chris Jester-Young Avatar answered Oct 01 '22 07:10

Chris Jester-Young