home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!randyd
- From: randyd@csd4.csd.uwm.edu (Randall Elton Ding)
- Newsgroups: comp.lang.pascal
- Subject: Re: need help with x to y power or 'setbit' function
- Date: 9 Nov 1992 21:30:45 GMT
- Organization: Computing Services Division, University of Wisconsin - Milwaukee
- Lines: 59
- Message-ID: <1dmla5INNovf@uwm.edu>
- References: <BxBDzx.AD3@javelin.sim.es.com>
- NNTP-Posting-Host: 129.89.7.4
-
- In article <BxBDzx.AD3@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
- >i'm using bits in an array of bytes as flags in the application i'm
- >developing. i bitest via bitest:=(byte and ($01 shl bit))<>0 but i'm having
- >trouble with my setbit and clearbit routines. my method of accomplishing
- >bitsetting was going to be done by testing the desired bit of x, and if the
- >result is false to add 2 to the bit power to x. clearbit would obviously be
- >just the converse.
- >
- >unfortunately, i can't figure out how to get x to the power of y on turbo
- >pascal 5.0. i have a real crummy book which says use "**" (which doesn't
- >work) or (raise power)*natural logarithm(x), which again doesn't produce the
- >desired result.
- >
- >i would gladly accept a simple setbit and clearbit routine; however, i would
- >also like to know how to raise to a power under this language.
- >
- >i appreciate and anticipate your response.
- >--
- > there is no religion when a man has good curry
- > -
- > tim clinkenpeel: aberrant analytical skeptical agnostic idealist.
- > -- i exclusively represent myself --
-
- Here are some functions I use to do just that...
-
- procedure chbits (var x: byte; mask,mode: byte);
- begin
- x:= ((x xor mode) and mask) xor x;
- end;
-
- procedure setbits (var x: byte; mask: byte);
- begin
- chbits (x,mask,$FF);
- end;
-
- procedure clearbits (var x: byte; mask: byte);
- begin
- chbits (x,mask,$00);
- end;
-
- function pwr (x,e: real): real;
- var
- result: real;
- p,n: integer;
-
- begin
- n:= round (e);
- if frac (e) = 0 then begin
- if n = 0 then result:= 1 else result:= x;
- p:= abs (n);
- while p > 1 do begin
- result:= result * x;
- dec (p);
- end;
- end
- else result:= exp (e * ln (abs (x) ) );
- if n < 0 then result:= 1/result;
- pwr:= result;
- end;
-