Mail Archives: djgpp/1997/05/14/08:05:41
From:  Shawn Hargreaves <shawn AT talula DOT demon DOT co DOT uk>

To:  djgpp AT delorie DOT com

MMDFWarning:  Parse error in original version of preceding line at punt2.mail.demon.net

Subject:  Re: Allegro fixed point variables

Date:  Wed, 14 May 1997 10:59:12 +0100

MIMEVersion:  1.0

MessageID:  <863609350.097657.0@[194.129.18.166]>

Ben N Shadwick writes:
> out Allegro, and I noticed in Allegro.txt it says (pertaining to fixed
> point numbers & angles):
>
> "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)?
In hex, a full circle is 0x100. Shift it into 16.16 format and you get
0x1000000. So an angle of 0x1000000 is the same as 0, 0x1000001 is the same
as 1, etc. In other words, you are only interested in the low 24 bits of
the value, so you can mask off the irrelevant information by writing (angle
& 0xFFFFFF).
For example, to test if two angles are the same, regardless of whether they
may have wrapped around a full circle, just write if ((angle1 & 0xFFFFFF)
== (angle2 & 0xFFFFFF)). That's a lot more efficient than working with
degrees, and having to use the modulus operator.

Shawn Hargreaves  shawn AT talula DOT demon DOT co DOT uk  http://www.talula.demon.co.uk
Beauty is a French phonetic corruption of a short cloth neck ornament
 Raw text 