home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / wct / bits.pas next >
Encoding:
Pascal/Delphi Source File  |  1991-04-20  |  1.3 KB  |  66 lines

  1. unit bits;
  2.  
  3. { Written by William C. Thompson }
  4.  
  5. { This unit is intended to be used in programs which do a lot
  6.   of bit manipulation. }
  7.  
  8. interface
  9.  
  10. const two:array[0..15] of word=(1,2,4,8,16,32,64,128,256,512,
  11.                                 1024,2048,4096,8192,16384,32768);
  12.  
  13. function bset(n:word; p:byte):word;
  14. function bclr(n:word; p:byte):word;
  15. function btst(n:word; p:byte):boolean;
  16. function asl(n:word; i:byte):word;
  17. function asr(n:word; i:byte):word;
  18. function lsl(n:word; i:byte):word;
  19. function lsr(n:word; i:byte):word;
  20.  
  21. implementation
  22.  
  23. function bset(n:word; p:byte):word;
  24. { sets the pth bit of n }
  25. begin
  26.   bset:=n or two[p]
  27. end;
  28.  
  29. function bclr(n:word; p:byte):word;
  30. { clears the pth bit of n }
  31. begin
  32.   bclr:=n and (65535-two[p])
  33. end;
  34.  
  35. function btst(n:word; p:byte):boolean;
  36. { returns if pth bit is 1 }
  37. begin
  38.   btst:=n and two[p]>0
  39. end;
  40.  
  41. function asl(n:word; i:byte):word;
  42. { performs i arithmetic left-shifts on n }
  43. begin
  44.   asl:=n*two[i]
  45. end;
  46.  
  47. function asr(n:word; i:byte):word;
  48. { performs i arithmetic right-shifts on n }
  49. begin
  50.   asr:=n div two[i]
  51. end;
  52.  
  53. function lsl(n:word; i:byte):word;
  54. { performs i logical left-shifts on n }
  55. begin
  56.   { As yet unfinished }
  57. end;
  58.  
  59. function lsr(n:word; i:byte):word;
  60. { performs i logical right-shifts on n }
  61. begin
  62.   { As yet unfinished }
  63. end;
  64.  
  65. end.
  66.