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

  1. /*
  2.  * File......: BITTEST.PRG
  3.  * Author....: Forest Belt, Computer Diagnostic Services, Inc.
  4.  * Date......: $Date:   15 Aug 1991 23:02:54  $
  5.  * Revision..: $Revision:   1.2  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/bittest.prv  $
  7.  * 
  8.  * This is an original work by Forest Belt and is placed in the
  9.  * public domain.
  10.  *
  11.  *   Execution times on 286/10Mhz clone machine          Execute times
  12.  *                                                       -------------
  13.  *   FT_ISBIT        Test any bit in a byte                     2 MSEC
  14.  *   FT_BITSET       Set one bit in a byte                      1 MSEC
  15.  *   FT_BITCLR       Clear one bit in a byte                    2 MSEC
  16.  *
  17.  *   FT_BYTEAND      AND two bytes together, return result     27 MSEC
  18.  *   FT_BYTEOR       OR two bytes together, return result      36 MSEC
  19.  *   FT_BYTEXOR      XOR two bytes together, return result     38 MSEC
  20.  *   FT_BYTENOT      Return one's complement of a byte         19 MSEC
  21.  *   FT_BYTENEG      Return two's complement of a byte         20 MSEC
  22.  *
  23.  *   FT_BYT2BIT      Return bit (binary) representation of byte
  24.  *   FT_BYT2HEX      Return hexadecimal representation of byte
  25.  *
  26.  * MODIFICATION HISTORY:
  27.  * ---------------------
  28.  *
  29.  * $Log:   E:/nanfor/src/bittest.prv  $
  30.  * 
  31.  *    Rev 1.2   15 Aug 1991 23:02:54   GLENN
  32.  * Forest Belt proofread/edited/cleaned up doc
  33.  * 
  34.  *    Rev 1.1   14 Jun 1991 19:51:02   GLENN
  35.  * Minor edit to file header
  36.  *
  37.  */
  38.  
  39.  
  40. #IFDEF FT_TEST
  41.   FUNCTION MAIN()
  42.  
  43.  
  44.      SET CONFIRM ON
  45.  
  46.      CLEAR
  47.      @ 1,2 TO 23,77 DOUBLE    // make a border box
  48.  
  49.      SETCOLOR("w+/n")
  50.      @ 1,5 SAY "[ BIT MANIPULATIONS DEMONSTRATOR - NANFOR.LIB ]"
  51.  
  52.      DO WHILE .t.       // MAIN LOOP
  53.  
  54.          @ 2,3 CLEAR TO 22,76    // clear the box
  55.  
  56.          SETCOLOR("n/w")         // reverse box for instructions
  57.          @ 5,5 CLEAR TO 20,74
  58.    
  59.          @  7, 10 SAY "DO YOU WANT TO DEMONSTRATE..."
  60.          @  9, 15 SAY "1 - Bit Test, Set, Clear"
  61.          @ 10, 15 SAY "2 - Bit-Wise Logical Manipulations"
  62.          @ 12, 15 SAY "0 - Exit from here"
  63.          @ 14, 10 SAY "Select by Number"
  64.    
  65.          nSlct := 0
  66.          DO WHILE (nSlct < 48 .OR. nSlct > 50)
  67.             nSlct := INKEY(0)
  68.          END
  69.    
  70.          IF nSlct == 48
  71.             EXIT
  72.          END
  73.    
  74.          DO WHILE .t.      // OPERATIONS LOOP
  75.    
  76.       
  77.             SETCOLOR("w/n")
  78.             @ 2,3 CLEAR TO 22,76    // clear the box
  79.  
  80.             @ 3,10 SAY ;
  81.                "Use numeric value of the byte(s) you wish to test.  This"
  82.             @ 4,10 SAY ;
  83.                "makes it easier to evaluate what's happening bit-wise."
  84.             @ 11,10 SAY "Leave blanks empty to Quit"
  85.  
  86.             cInbyte1 := SPACE(3)
  87.  
  88.             IF nSlct == 49
  89.                cInbyte2 := SPACE(1)
  90.                @ 6,15 SAY "Byte value:   " GET cInbyte1 picture "###"
  91.                @ 8,15 SAY "Bit position: " GET cInbyte2 picture "#"
  92.             ELSE
  93.                cInbyte2 := SPACE(3)
  94.                @ 6,10 SAY "First byte value:  " GET cInbyte1 picture "###"
  95.                @ 8,10 SAY "Second byte value: " GET cInbyte2 picture "###"
  96.             END
  97.  
  98.             READ
  99.  
  100.             IF EMPTY(cInbyte1) .AND. EMPTY(cInbyte2)
  101.                EXIT
  102.             END
  103.  
  104.             @ 3,10 SAY SPACE(60)    // Cleanup display
  105.             @ 4,10 SAY SPACE(60)
  106.             @ 11,10 SAY SPACE(40)
  107.  
  108.             // Validations
  109.             IF EMPTY(cInbyte1)
  110.                QQOUT(CHR(7))
  111.                @ 6,36 SAY "NEED BOTH VALUES"
  112.                INKEY(3)
  113.                LOOP
  114.             END
  115.  
  116.             IF (nSlct == 49 ;
  117.                   .AND. (VAL(cInbyte1) > 255 .OR. VAL(cInbyte2) > 7)) ;
  118.                   .OR. (nSlct == 50 ;
  119.                   .AND. (VAL(cInbyte1) > 255 .OR. VAL(cInbyte2) > 255))
  120.                QQOUT(CHR(7))
  121.                @ 6,36 SAY "0 - 255"
  122.                IF nSlct == 49
  123.                   @ 8,36 SAY "0 - 7"
  124.                END
  125.                INKEY(3)
  126.                LOOP
  127.             END
  128.  
  129.             // assign values in correct types
  130.             nBytval := VAL(cInbyte1)
  131.             nBitpos := VAL(cInbyte2)
  132.  
  133.             IF nSlct == 49
  134.                cByte1 := CHR(nBytval)
  135.             ELSE
  136.                cByte1 := cByte3 := cByte5 := cByte7 := cByte8 := CHR(nBytval)
  137.                cByte2 := cByte4 := cByte6 := CHR(nBitpos)
  138.             END
  139.  
  140.             // Headings
  141.             @ 3,28 SAY "Bit No. 7654 3210"
  142.             @ 3,49 SAY "Hex"
  143.             @ 3,55 SAY "Dec"
  144.             @ 3,61 SAY "ASCII"
  145.          
  146.             @ 4,36 SAY REPLICATE(CHR(25),4)+" "+REPLICATE(CHR(25),4)
  147.             @ 4,49 SAY "---"
  148.             @ 4,55 SAY "---"
  149.             @ 4,61 SAY "-----"
  150.  
  151.             @ 6,36 SAY FT_BYT2BIT(cByte1)    // full display 1st value
  152.             @ 6,49 SAY FT_BYT2HEX(cByte1)
  153.             @ 6,55 SAY STR(ASC(cByte1),3)
  154.             @ 6,63 SAY cByte1
  155.       
  156.             IF nSlcT == 49
  157.  
  158.                // full displays of manipulated bytes
  159.  
  160.                @ 10,22 SAY "TEST Bit " + STR(nBitpos,1)
  161.                @ 10,38 SAY IIF(FT_ISBIT(cByte1,nBitpos), ;
  162.                   "* It is SET *","* It is CLEAR *")
  163.  
  164.                cByte3 := FT_BITSET(cByte1,nBitpos)
  165.                @ 12,22 SAY "SET Bit " + STR(nBitpos,1)
  166.                @ 12,36 SAY FT_BYT2BIT(cByte3)
  167.                @ 12,49 SAY FT_BYT2HEX(cByte3)
  168.                @ 12,55 SAY STR(ASC(cByte3),3)
  169.                @ 12,63 SAY cByte3
  170.  
  171.                cByte5 := FT_BITCLR(cByte1,nBitpos)
  172.                @ 14,22 SAY "CLEAR Bit " + STR(nBitpos,1)
  173.                @ 14,36 SAY FT_BYT2BIT(cByte5)
  174.                @ 14,49 SAY FT_BYT2HEX(cByte5)
  175.                @ 14,55 SAY STR(ASC(cByte5),3)
  176.                @ 14,63 SAY cByte5
  177.          
  178.             ELSE
  179.       
  180.                @ 8,36 SAY FT_BYT2BIT(cByte2)    // full display 2nd value
  181.                @ 8,49 SAY FT_BYT2HEX(cByte2)
  182.                @ 8,55 SAY STR(ASC(cByte2),3)
  183.                @ 8,63 SAY cByte2
  184.  
  185.                // full displays of manipulated bytes
  186.  
  187.                cByte1 := FT_BYTEAND(cByte1,cByte2)
  188.                @ 10,28 SAY "ANDed"
  189.                @ 10,36 SAY FT_BYT2BIT(cByte1)
  190.                @ 10,49 SAY FT_BYT2HEX(cByte1)
  191.                @ 10,55 SAY STR(ASC(cByte1),3)
  192.                @ 10,63 SAY cByte1
  193.  
  194.                cByte3 := FT_BYTEOR(cByte3,cByte4)
  195.                @ 12,28 SAY "ORed"
  196.                @ 12,36 SAY FT_BYT2BIT(cByte3)
  197.                @ 12,49 SAY FT_BYT2HEX(cByte3)
  198.                @ 12,55 SAY STR(ASC(cByte3),3)
  199.                @ 12,63 SAY cByte3
  200.  
  201.                cByte5 := FT_BYTEXOR(cByte5,cByte6)
  202.                @ 14,28 SAY "XORed"
  203.                @ 14,36 SAY FT_BYT2BIT(cByte5)
  204.                @ 14,49 SAY FT_BYT2HEX(cByte5)
  205.                @ 14,55 SAY STR(ASC(cByte5),3)
  206.                @ 14,63 SAY cByte5
  207.  
  208.                cByte7 := FT_BYTENOT(cByte7)
  209.                @ 16,15 SAY "First byte - NOTed"
  210.                @ 16,36 SAY FT_BYT2BIT(cByte7)
  211.                @ 16,49 SAY FT_BYT2HEX(cByte7)
  212.                @ 16,55 SAY STR(ASC(cByte7),3)
  213.                @ 16,63 SAY cByte7
  214.  
  215.                cByte8 := FT_BYTENEG(cByte8)
  216.                @ 18,15 SAY "First byte - NEGed"
  217.                @ 18,36 SAY FT_BYT2BIT(cByte8)
  218.                @ 18,49 SAY FT_BYT2HEX(cByte8)
  219.                @ 18,55 SAY STR(ASC(cByte8),3)
  220.                @ 18,63 SAY cByte8
  221.          
  222.             END         // nSlct 49 or 50?
  223.  
  224.  
  225.             @ 21,10 SAY "Press a Key to Continue"
  226.             INKEY(0)
  227.  
  228.          END            // End OPERATIONS LOOP
  229.    
  230.      END       // End MAIN LOOP
  231.  
  232.      SETCOLOR("w/n")
  233.      CLEAR
  234.   RETURN ( NIL )
  235.  
  236. #endif
  237.  
  238.  
  239.  
  240.