home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / pascal / 6440 < prev    next >
Encoding:
Internet Message Format  |  1992-11-09  |  2.2 KB

  1. Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!randyd
  2. From: randyd@csd4.csd.uwm.edu (Randall Elton Ding)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: need help with x to y power or 'setbit' function
  5. Date: 9 Nov 1992 21:30:45 GMT
  6. Organization: Computing Services Division, University of Wisconsin - Milwaukee
  7. Lines: 59
  8. Message-ID: <1dmla5INNovf@uwm.edu>
  9. References: <BxBDzx.AD3@javelin.sim.es.com>
  10. NNTP-Posting-Host: 129.89.7.4
  11.  
  12. In article <BxBDzx.AD3@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
  13. >i'm using bits in an array of bytes as flags in the application i'm
  14. >developing.  i bitest via bitest:=(byte and ($01 shl bit))<>0 but i'm having
  15. >trouble with my setbit and clearbit routines.  my method of accomplishing
  16. >bitsetting was going to be done by testing the desired bit of x, and if the
  17. >result is false to add 2 to the bit power to x.  clearbit would obviously be
  18. >just the converse.
  19. >
  20. >unfortunately, i can't figure out how to get x to the power of y on turbo
  21. >pascal 5.0.  i have a real crummy book which says use "**" (which doesn't
  22. >work) or (raise power)*natural logarithm(x), which again doesn't produce the
  23. >desired result.
  24. >
  25. >i would gladly accept a simple setbit and clearbit routine; however, i would
  26. >also like to know how to raise to a power under this language.
  27. >
  28. >i appreciate and anticipate your response.
  29. >-- 
  30. >           there is no religion when a man has  good curry
  31. >                       -
  32. >      tim clinkenpeel: aberrant analytical skeptical agnostic idealist.
  33. >             -- i exclusively represent myself --
  34.  
  35. Here are some functions I use to do just that...
  36.  
  37. procedure chbits (var x: byte; mask,mode: byte);
  38.   begin
  39.     x:= ((x xor mode) and mask) xor x;
  40.   end;
  41.  
  42. procedure setbits (var x: byte; mask: byte);
  43.   begin
  44.     chbits (x,mask,$FF);
  45.   end;
  46.  
  47. procedure clearbits (var x: byte; mask: byte);
  48.   begin
  49.     chbits (x,mask,$00);
  50.   end;
  51.  
  52. function pwr (x,e: real): real;
  53.   var
  54.     result: real;
  55.     p,n: integer;
  56.  
  57.   begin
  58.     n:= round (e);
  59.     if frac (e) = 0 then begin
  60.       if n = 0 then result:= 1 else result:= x;
  61.       p:= abs (n);
  62.       while p > 1 do begin
  63.         result:= result * x;
  64.         dec (p);
  65.       end;
  66.     end
  67.     else result:= exp (e * ln (abs (x) ) );
  68.     if n < 0 then result:= 1/result;
  69.     pwr:= result;
  70.   end;
  71.