Newsgroups: comp.os.msdos.djgpp
From: tob AT world DOT std DOT com
Subject: Re: Allegro fixed point variables
Message-ID:
Sender: tob AT world DOT std DOT com (Tom Breton)
Reply-To: tob AT world DOT std DOT com
Organization: BREnterprises
References: <19970513 DOT 174418 DOT 12478 DOT 0 DOT bshadwick AT juno DOT com>
Date: Wed, 14 May 1997 18:34:54 GMT
Lines: 33
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Precedence: bulk
bshadwick AT juno DOT com (Ben N Shadwick) writes:
> "Angles are represented in a binary format with 256 equal to a full
> circle,
> 64 being a right angle and so on. This has the advantage that a simple
> bitwise 'and' can be used to keep the angle within the range zero to a
> full
> circle, eliminating all those tiresome 'if (angle >= 360)' checks."
>
> Call me stupid, but how exactly would I implement a bitwise and to get it
> to do that (in such a way that it would be more practical than the above
> check)?
const int fixed_point_unit = 0x10000;
const int full_circle = fixed_point_unit * 256;
inline int
clip_angle( int angle )
{
/* This operation becomes a bitwise AND. */
return angle % full_circle;
}
/* Or equivalently... */
inline int
clip_angle( int angle )
{
assert( ( full_circle & ( full_circle - 1 ) ) == 0 );
return angle & ( full_circle - 1 );
}
Tom