Author: Walter Faxon
Date: 21:32:27 01/05/03
Go up one level in this thread
On January 05, 2003 at 21:50:08, Robert Hyatt wrote:
>On January 05, 2003 at 15:55:25, Walter Faxon wrote:
>
>>On January 04, 2003 at 23:32:43, Robert Hyatt wrote:
>>
>>>On January 04, 2003 at 15:32:40, Walter Faxon wrote:
>>>
>>>>On January 03, 2003 at 19:52:06, Matt Taylor wrote:
>>
>><snip>
>>
>>>>>The compiler -should- recognize that 2 * to = to << 1
>>>>
>>>>ONLY IF 'to' is guaranteed positive. Try: "2 * (unsigned)to". Or explicit
>>>>shifting is always ok in my book. Use a comment or #if'ed-out debug code if you
>>>>want to keep things clear.
>>>>
>>>
>>>I'm missing something...
>>>
>>>-1<<1 is the same thing as -1*2... whether it is positive or negative does
>>>not matter at all.
>>
>><snip>
>>
>>Sorry. Leftshift always ok for integer multiply by power of 2. It's rightshift
>>for divide by power of 2 that requires an unsigned operand. Mea culpa.
>>
>>-- Walter
>
>
>Actually right shift is also ok if the initial value is <= -2... :)
Arithmetic right shift results in a divide by the given power of two, "rounding
down" rather than the expected "rounding towards zero", e.g.,
( n / 2) == result
( 5 >> 1) == 2
( 4 >> 1) == 2
( 3 >> 1) == 1
( 2 >> 1) == 1
( 1 >> 1) == 0
( 0 >> 1) == 0
(-1 >> 1) == -1
(-2 >> 1) == -1
(-3 >> 1) == -2
(-4 >> 1) == -2
(-5 >> 1) == -3
So, yes, it always works, but for negative numbers it might not produce what you
expect.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.