www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/14/16:39:16

Newsgroups: comp.os.msdos.djgpp
From: tob AT world DOT std DOT com
Subject: Re: Allegro fixed point variables
Message-ID: <EA6oy7.D2I@world.std.com>
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

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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019