home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / BITSET.PRG < prev    next >
Text File  |  1991-08-16  |  3KB  |  92 lines

  1. /*
  2.  * File......: BITSET.PRG
  3.  * Author....: Forest Belt, Computer Diagnostic Services, Inc.
  4.  * Date......: $Date:   15 Aug 1991 23:02:52  $
  5.  * Revision..: $Revision:   1.2  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/bitset.prv  $
  7.  * 
  8.  * This is an original work by Forest Belt and is placed in the
  9.  * public domain.
  10.  *
  11.  * Modification history:
  12.  * ---------------------
  13.  *
  14.  * $Log:   E:/nanfor/src/bitset.prv  $
  15.  * 
  16.  *    Rev 1.2   15 Aug 1991 23:02:52   GLENN
  17.  * Forest Belt proofread/edited/cleaned up doc
  18.  * 
  19.  *    Rev 1.1   14 Jun 1991 19:51:00   GLENN
  20.  * Minor edit to file header
  21.  * 
  22.  *    Rev 1.0   01 Apr 1991 01:00:42   GLENN
  23.  * Nanforum Toolkit
  24.  *
  25.  */
  26.  
  27.  
  28.  
  29. /*  $DOC$
  30.  *  $FUNCNAME$
  31.  *     FT_BITSET()
  32.  *  $CATEGORY$
  33.  *     String
  34.  *  $ONELINER$
  35.  *     Set selected bit in a byte
  36.  *  $SYNTAX$
  37.  *     FT_BITSET( <cByte>, <nBitPos> ) -> cByte
  38.  *  $ARGUMENTS$
  39.  *     <cByte> is a character from CHR(0) to CHR(255).
  40.  *
  41.  *     <nBitPos> is a number from 0 to 7 conforming to standard right-to-left
  42.  *     bit numbering convention and representing the position of the bit
  43.  *     within the byte.
  44.  *  $RETURNS$
  45.  *     Returns new byte, with designated bit set.  If parameters are faulty,
  46.  *     returns NIL.
  47.  *  $DESCRIPTION$
  48.  *     In effect, ORs argument byte with a byte that has only the target bit
  49.  *     set.  If bit is already set, it remains set.
  50.  *     Note: Calls FT_ISBIT() which is also in this Library.
  51.  *
  52.  *     This function is presented to illustrate that bit-wise operations
  53.  *     are possible with Clipper code.  For greater speed, write .C or
  54.  *     .ASM versions and use the Clipper Extend system.
  55.  *  $EXAMPLES$
  56.  *     This code would set bit 4 in a byte represented by CHR(107):
  57.  *
  58.  *          cNewbyte := FT_BITSET( CHR(107), 4 )
  59.  *          ? ASC( cNewbyte )             // result: 123
  60.  *          ? cNewbyte                    // result: '{'
  61.  *
  62.  *
  63.  *     This code would set bit 5 in the byte represented by the letter 'A'.
  64.  *
  65.  *          ? FT_BITSET( 'A', 5 )         // result: 'a'
  66.  *                                        // bit 5 set
  67.  *
  68.  *     For a demonstration of Clipper bit manipulations, compile and
  69.  *     link the program BITTEST.PRG in the Nanforum Toolkit source code.
  70.  *  $SEEALSO$
  71.  *     FT_BITCLR() FT_ISBIT()
  72.  *  $END$
  73.  */
  74.  
  75. FUNCTION FT_BITSET(cInByte, nBitpos)
  76.  
  77.   LOCAL cByte
  78.  
  79.   IF valtype(cInbyte) != "C" .or. valtype(nBitpos) != "N"  // parameter check
  80.      cByte := NIL
  81.   ELSE
  82.      IF (nBitPos > 7) .or. (nBitPos < 0) .or. (nBitPos != int(nBitPos))
  83.         cByte := NIL
  84.      ELSE
  85.         cByte := iif( FT_ISBIT(cInByte, nBitpos), cInByte, ;
  86.                        chr(asc(cInByte) + (2 ^ nBitpos)))
  87.      ENDIF
  88.   ENDIF
  89.  
  90. RETURN cByte
  91.  
  92.