tags:

views:

479

answers:

5

I am doing this..

value >> 3;

It is always going toward negative side.How do I round toward zero with right shift division?

+1  A: 

I do this:

(value + 4) >> 3
Adam Pierce
But the positives when divided are going up by 1 :(
RealHIFIDude
Perhaps I didn't understand your question properly. If you change it to ((value + 3) >> 3) do you get what you want ?
Adam Pierce
+3  A: 

Do something conditionally depending on whether your value is positive or negative.

if( value < 0 ) {
    -((-value) >> 3);
}
else {
    value >> 3;
}
indiv
+3  A: 

Try the following expression instead:

(value < 0) ? -((-value) >> 3) : value >> 3;

That will force a negative number to be positive first so that it round towards zero, then changes the result back to negative.

paxdiablo
A: 

You are encountering 'signed' shifting, when what you seem to want is unsigned shifting. Try casting it to unsigned first, like this

x = ((unsigned) x) >> 3;

.. or you could just use division.

JustJeff
A: 

Gez, the answers were pretty bad ; you want to solve that without branching, but without breaking your positive numbers either.

Here it is : (int)(value+(((unsigned)value)>>31)) >> 3

The cast to (unsigned) is required to perform a logical shift and obtain just the sign bit, then we need to cast back to (int) to perform an arithmetic right shift.

The code above made the assumption that your int data type is 32 bits, you should of course use data types such as int32_t in such cases.

Alexis Naveros
Correction!For a division by 8 ( or shift by 3 ), you would actually need to add 7 times the sign bit ( or numerator - 1 ) ; not once as in the example above, which would work for a shift by 1 ( or a division by 2 ).So, in other words, something like :uint32_t signbit=((uint32_t)value)>>31;result = ((int32_t)(value+(signbit<<shift)-signbit))>>shift;
Alexis Naveros