Mail Archives: djgpp/1997/05/14/16:39:16
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 -