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

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!eff!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!sgiblab!munnari.oz.au!metro!grivel!metz.une.edu.au!ddavidso
  2. From: ddavidso@metz.une.edu.au (Dean Davidson)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: need help with x to y power or 'setbit' function
  5. Message-ID: <2461@grivel.une.edu.au>
  6. Date: 12 Nov 92 01:24:09 GMT
  7. References: <BxBDzx.AD3@javelin.sim.es.com>
  8. Sender: usenet@grivel.une.edu.au
  9. Organization: University of New England, Armidale, Australia
  10. Lines: 81
  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. Here is the unit I use:
  21.  
  22. Thanks to Trevor Carlsen for some of these
  23.  
  24.  
  25. unit bituni;
  26.  
  27. interface
  28.  
  29. {32 bit (longint) routines}
  30. function  b_test(      numb : longint ; posn : byte) : boolean;
  31. procedure b_set(   var numb : longint ; posn : byte);
  32. procedure b_toggle(var numb : longint ; posn : byte);
  33. procedure b_reset( var numb : longint ; posn : byte);
  34.  
  35. {8 bit (byte) routines}
  36. function BitIsSet(numb,bitnumb : byte): boolean;
  37. {To test a bit in a byte - Tests bit bitnumb in numb}
  38. procedure SetBit(var numb : byte; bitnumb : byte);
  39. {To set a bit in a byte - Sets bit bitnumb in numb}
  40. procedure ResetBit(var numb : byte; bitnumb : byte);
  41. {To reset a bit in a byte - Resets bit bitnumb in numb}
  42.  
  43. implementation
  44.  
  45. function b_test(numb : longint ; posn : byte) : boolean;
  46. begin
  47.   b_test := odd(numb shr posn);
  48. end;
  49.  
  50. procedure b_set(var numb : longint ; posn : byte);
  51. begin
  52.   numb := numb or (1 shl posn);
  53. end;
  54.  
  55. procedure b_toggle(var numb : longint ; posn : byte);
  56. begin
  57.   numb := numb xor (1 shl posn);
  58. end;
  59.  
  60. procedure b_reset(var numb : longint ; posn : byte);
  61. begin
  62.   numb := (numb or (1 shl posn)) xor (1 shl posn);
  63. end;
  64.  
  65. {-----BYTE OPS-----}
  66.  
  67. function BitIsSet(numb,bitnumb : byte): boolean;
  68. {To test a bit in a byte - Tests bit bitnumb in numb}
  69. begin
  70.   BitIsSet := odd(numb shr bitnumb);
  71. end;
  72.  
  73. procedure SetBit(var numb : byte; bitnumb : byte);
  74. {To set a bit in a byte - Sets bit bitnumb in numb}
  75. begin
  76.   numb := numb or (1 shl bitnumb);
  77. end;
  78.  
  79. procedure ResetBit(var numb : byte; bitnumb : byte);
  80. {To reset a bit in a byte - Resets bit bitnumb in numb}
  81. begin
  82.   numb := numb and not (1 shl bitnumb);
  83. end;
  84.  
  85. begin
  86. end.
  87.  
  88. -- 
  89. Dean Davidson                                 ddavidso@metz.une.oz.au
  90. Dept Psychology                               Phone 61 67 73 2585
  91. University of New England                     Fax   61 67 72 9816
  92. Armidale NSW 2351 Australia                   VK2 ZID
  93.