Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about an efficient way to change the bits of a number.

Author: Tim Foden

Date: 04:17:34 07/27/03

Go up one level in this thread


On July 27, 2003 at 06:51:51, Uri Blass wrote:

>In movei there are cases when I want to change 2 bits of a number to be
>identical to the bits of another number.
>
>Let say I want b=a in the 2^0 bit and the 2^8 bit
>
>The simple way is
>
>if (a&256)
>b|=256;
>else
>b&=~256;
>if (a&1)
>b|=1;
>else
>b&=~1;
>
>a faster way that I could think about is:
>
>b|=(a&257);
>b&=~(257-(a&257));
>
>Is it the best way or maybe it is better to change the last line and have
>b|=(a&257);
>b&=~(257&(~(a&257)))
>
>Uri

Ricardo's solution looks good.  It may also be fast due to parallisation.

In the old days when I used to program in machine code (e.g. Z80), an algorithm
like this was used (it was fast (3 instructions) and used few registers)...

b ^= a;
b &= ~257;
b ^= a;

Nowdays this may be slower than the alternatives due to it's serial nature.

Cheers, Tim.



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.