Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Determining location of LSB/MSB

Author: Omid David Tabibi

Date: 05:15:18 02/09/04

Go up one level in this thread


On February 09, 2004 at 05:09:07, Renze Steenhuisen wrote:

>
>Hi there!
>
>does anyone know the fastest way to determine the lcoation of the Least/Most
>Significant Bit, and to clear that bit?
>
>In my case the most important platforms (at the moment) are Pentium III and IV.
>

For 32 bit data:


__forceinline int findFirstBitTrue(int a) {
	__asm   bsf		eax, dword ptr[a]
};

__forceinline int findLastBitTrue(int a) {
	__asm   bsr		eax, dword ptr[a]
};


If you know that a certain bit is 1, and you want to clear it, probably the best
way to do that is:

#define XorBit(a,i)		(a ^= (1 << i))

Otherwise, if you don't know whether a bit is set or not, you can use the
following macros to set it to 1 or 0:

#define SetBit(a,i)		(a |= (1 << i))
#define DelBit(a,i)		(a &= ~(1 << i))

Or in assembly:

__forceinline void setBitTrue(int *a, int i) {
	__asm {
		mov		ebx, dword ptr[a]
		mov		eax, dword ptr[i]
		bts		[ebx], eax
	}
};

__forceinline void setBitFalse(int *a, int i) {
	__asm {
		mov		ebx, dword ptr[a]
		mov		eax, dword ptr[i]
		btr		[ebx], eax
	}
};

But here I don't think the assembly version will be faster than the C macro.


>Thanks!
>
>Renze



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.