home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progpas / tspas18.arj / TSUNTB.INT < prev    next >
Text File  |  1990-02-25  |  5KB  |  128 lines

  1. {$B-,D-,F-,I+,N-,R-,S+,V+}
  2.  
  3. (*
  4. Timo Salmi UNiT B
  5. A Turbo Pascal 5.0 unit for (a) bit (of) manipulation
  6. All rights reserved 22-Jul-89,
  7. Updated 26-Jul-89, 19-Aug-89, 18-Oct-89
  8.  
  9. Here are some power functions, bit manipulation and base conversions.
  10. There is nothing really novel about them, just that I have needed them
  11. myself, and thought to make some available to [a/be]muse the general public.
  12. Some of the functions are, however, hopefully both smaller faster than the
  13. corresponding routines in average Turbo Pascal guides, or have features
  14. that are normally lacking.
  15.  
  16. This unit may be used and distributed freely for PRIVATE, NON-COMMERCIAL,
  17. NON-INSTITUTIONAL purposes, provided it is not changed in any way. For
  18. ANY other usage, contact the author.
  19.  
  20. The units are under development. Comments and contacts are solicited. If
  21. you have any questions, please do not hesitate to use electronic mail for
  22. communication.
  23. InterNet address: ts@chyde.uwasa.fi         (preferred)
  24. Funet address:    GADO::SALMI
  25. Bitnet address:   SALMI@FINFUN
  26. FidoNet address:  2:515/1 (Micro Maniacs Opus, To: Timo Salmi)
  27.  
  28. The author shall not be liable to the user for any direct, indirect or
  29. consequential loss arising from the use of, or inability to use, any unit,
  30. program or file howsoever caused. No warranty is given that the units and
  31. programs will work under all circumstances.
  32.  
  33. Timo Salmi
  34. Professor of Accounting and Business Finance
  35. School of Business Studies, University of Vaasa
  36. P.O. BOX 297, SF-65101 Vaasa, Finland
  37. *)
  38.  
  39. unit TSUNTB;
  40.  
  41. (* ======================================================================= *)
  42.                           interface
  43. (* ======================================================================= *)
  44.  
  45. uses Dos;
  46.  
  47. (*
  48. If one wants to test and compare speeds of various procedures and functions
  49. one needs a procedure to give the elapsed time. TIMERFN does that. It is
  50. very simple. It just gives the seconds elapsed since midnight to the apparent
  51. precision of a hundreth of a second.
  52. *)
  53.  
  54. (* Time elapsed since midnight *)
  55. function TIMERFN : real;
  56.  
  57. (*
  58. Pascal lacks a power function and therefore one has to build it oneself.
  59. The simplest way to calculate a power function is Exp(exponent*Ln(number)),
  60. which many guides suggest. It is unsatisfactory, however, from the point
  61. of view that powers of zero or negative numbers cause an error. The
  62. POWERGFN does not have this hitch, but is, of course, slightly slower.
  63. Invalid operations such as -0.55 to -3.5 are detected and running halted.
  64. In special cases a power function can be made very fast. TWOTOFN is such a
  65. function raising two to a power.
  66. *)
  67.  
  68. (* Raise a positive number to a power the traditional way *)
  69. function POWERFN (number, exponent : real) : real;
  70.  
  71. (* Raise any number to a power, the improved function *)
  72. function POWERGFN (number, exponent : real) : real;
  73.  
  74. (* Raise a longint to a power, fast; Do not use negative exponents *)
  75. function POWERLFN (number, exponent : longint) : longint;
  76.  
  77. (* Raise two to a power, that is 2^exponent, very fast *)
  78. function TWOTOFN (exponent : word) : word;
  79.  
  80. (* Raise sixteen to a power, that is 16^exponent, very fast *)
  81. function R16TOFN (exponent : word) : word;
  82.  
  83. (*
  84. The next function is specialized, but occasionally very useful. As is
  85. known a word is made up of 16 bits numbered from 0 to 15. The following
  86. function establishes whether a particular bit is on or off in a word
  87. *)
  88.  
  89. (* Is an individual bit on in a word, bits are numbered from 0-15 as usual *)
  90. function BITONFN (status : word; bit : byte) : boolean;
  91.  
  92. (*
  93. Base conversion is a quite commonly occurring task in programming. It
  94. involves a similar problem to the one explained in discussing the
  95. routines for raising a number to a power: The routines can be made
  96. general, or they can be made fast. Here are some of the routines.
  97. *)
  98.  
  99. (* Convert a number from any base to any base (2-36) *)
  100. function CONVBFN (number : string; frombase, tobase : byte) : string;
  101.  
  102. (* Convert a binary string fast to a decimal word *)
  103. function BINDECFN (binary : string) : word;
  104.  
  105. (* Convert a decimal word to a binary string fast,
  106.    retained for compatibility with earlier versions *)
  107. function DECBINFN (decimal : word) : string;
  108.  
  109. (* Convert a decimal word to a binary string very fast *)
  110. function BINFN (decimal : word) : string;
  111.  
  112. (* Convert a decimal longint to a binary string fast *)
  113. function LBINFN (decimal : longint) : string;
  114.  
  115. (* Convert a hexadecimal string fast to a decimal word *)
  116. function HEXDECFN (hexadecimal : string) : word;
  117.  
  118. (* Convert a decimal word to a hexadecimal string fast,
  119.    retained for compatibility with earlier versions *)
  120. function DECHEXFN (decimal : word) : string;
  121.  
  122. (* Convert a decimal word to a hexadecimal string very fast *)
  123. function HEXFN (decimal : word) : string;
  124.  
  125. (* Convert a decimal longint to a hexadecimal string fast *)
  126. function LHEXFN (decimal : longint) : string;
  127.  
  128.