Path: chuka.playstation.co.uk!news From: Nick Slaven Newsgroups: scee.yaroze.beginners Subject: Re: Clever bit manipulation routines? Date: Thu, 23 Jul 1998 23:38:41 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 47 Message-ID: <35B7BB71.D869219@compuserve.com> References: <35B79502.BC7489D6@nospam.easynet.co.uk> NNTP-Posting-Host: ld52-142.lon.compuserve.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.04 [en] (Win95; I) Philip Gooch wrote: > > Hello folks > > I've got a few lines of code that look ugly and wondered if there's a > clever bit manipulation routine using shifting, ANDing and complement or > something to achieve the same thing. I'm sure there's a way, just can't > figure it out. > > Basically: > > if (gPlayer.turningAngle > 45) gPlayer.turningAngle = 45; > if (gPlayer.turningAngle < -45) gPlayer.turningAngle = -45; > > I'm convinced there's something out there that will handle both of these > in one line, something like > (~(gPlayer.turningAngle) & 0x2d) >> something. Though obviously not > this! > > Do you see what I'm getting at? Something that will leave the variable > unchanged unless it is greater or less than a certain value, in which > case it becomes that value. > > Anyone have any ideas? > > Cheers > > Phil 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) cheers Nick (S)