Path: chuka.playstation.co.uk!news From: Toby Hutton Newsgroups: scee.yaroze.beginners Subject: Re: Clever bit manipulation routines? Date: 24 Jul 1998 12:05:57 +1000 Organization: PlayStation Net Yaroze (SCEE) Lines: 42 Sender: thutton@TECH10 Message-ID: References: <35B79502.BC7489D6@nospam.easynet.co.uk> <35B7BB71.D869219@compuserve.com> NNTP-Posting-Host: 203.103.154.235 X-Newsreader: Gnus v5.3/Emacs 19.34 Nick Slaven writes: > how about the limitRange macro (tho its not bit manipulation) > like this > > limitRange(gPlayer.turningAngle,-45,45); > > its defined in libps.h & is coded as follows. > > #define limitRange(x, l, h) ((x)=((x)<(l)?(l):(x)>(h)?(h):(x))) > > The A=B?C:D; construct you find here, (if you're familiar with it > ignore the rest) takes the argument in B and if it is nonzero (ie true) > sticks the value of C in A, if B is zero (ie false) the value of D is > placed in A. B can be a single variable or any logical expression (like > (gPlayer.turningAngle<-45) for instance) This is probably the easiest way for arbitrary values. I'd also suggest you use more sensible values for your angles if possible. Looks like turningAngle is indeed an angle, right? And it looks like you're using 360 degrees as a full circle, and you want to limit the turning angle to an eighth of that, 45 degrees. The libraries often use 12 bit fixed point math. This means that 4096 is 1, 2048 is 1/2 and 512 will be an eighth. 512 in hex is 0x0200 and 511 is 0x01ff. To limit a value between 0 and +511 you simply: val &= 0x1ff; If 4096 is too high a resolution, try 256. It's a similar resolution to 360. An eighth of 256 is 32, and you could use: val &= 0x1f; This won't work for negative numbers. If you wanted to limit the range between -32 and 32 I'd do it by making the range 0-64 and then subtracting 32, or perhaps val = ((val + 0x20) & 0x3f) - 0x20; There really is no reason to use 360 degrees in a circle, except to satisfy any preconcieved notions that a 'right-angle' is 90 degrees. For computer science, 360 degrees is a not a great number as it isn't a power of 2. -- Toby.