BIT-Func

 

 

NOTE - Use your Browsers BACK Button to return to prior page or CLICK here.

   BIT-Func - XOR/OR/AND Demonstration (Bit Manipulations)
Zflamingo.jpg (26349 bytes) Bit manipulation is very common in low level languages such as Assembler or even C... setting bit values on or off can be quite handy... it can convert a character to upper or lower case... values can be exchanged... masks can determine validity of a value... etc... The BIT-Func simply highlights the results of the XOR, OR and AND operations... more below... this page also features flamingos and ostriches.

You can find the BIT-Func function in my custom SQR library TDFUNC.SQC

For more practical bit manipulations see the BOOL-Func also in TDFUNC.SQC

   NL-Func Parameters - Convert Number to Text Literal
 
Input    - Boolean Bit Operation (AND, OR, XOR)
Input    - Mask String (Binary-Based Character String)
Input    - Object String (Binary Based Character String)
Output  - Result Binary Based String (after applying mask)

 

To utilize bit operations effectively you need to understand the results of
each bit operation. The operations are AND, OR and XOR (Exclusive OR).
Below is a grid showing all possible bit results for each operation.

OPERATION XOR AND OR
Mask 0 0 1 1 0 0 1 1 0 0 1 1
Object 0 1 0 1 0 1 0 1 0 1 0 1
Result 0 1 1 0 0 0 0 1 0 1 1 1

   EXAMPLE (Converting ASCII lowercase to uppercase):
Let's assume we have a one-character field with the value 'a'. The ascii value of 'a' is 97 (or hex 61)... in binary this field is '01100001'. Let's use the AND operation to turn OFF bit '00100000' (which is decimal 32).

Using the grid above we see '01100001' AND '00100000' yields '01000001'.

This can also be represented as: 97 AND 32 = 65.

65 is the decimal code (in ASCII) for 'A' (uppercase). By turning the 3rd bit OFF we've converted the character to uppercase (From 'a' to 'A').

To accomplish the reverse we need to turn on the 3rd bit. For instance:

'01000001' OR '00100000' yields '01100001' (Binary Representation)

65 OR 32 = 97 (Decimal Representation) Note the decimal version appears to
be adding 65 and 32 to give 97. Not so... if we were to attempt this operation
on a character that were already in lowercase we would get the following:

97 OR 32 = 97 Notice the values weren't added. The OR operation ensures the 3rd bit is ON - it doesn't add the value itself. The IBM Principles of Operation Manual has a good explanation of bit operations.

   BIT-Func - Source Code
!**********************************************************************
!*       Bit Manipulation Functions - AND/OR/XOR                      *
!**********************************************************************
!*                                                                    *
!*        INPUT: $I_op      - Operation - AND / OR / XOR              *
!*               $I_mask    - Input  Binary String 1 (Mask)           *
!*               $I_string  - Input  Binary String 2 (Object)         *
!*       OUTPUT: $O_string  - Output Binary String                    *
!*                                                                    *
!*               To de-activate this function:                        *
!*               #define BIT_Func_Remove                              *
!*                                                                    *
!**********************************************************************
!*                                                                    *
!*      EXAMPLE: do BIT-Func('XOR', '01100001', '00100000', $res_xor) *
!*               do BIT-Func('AND', '00001111', '01010101', $res_and) *
!*               do BIT-Func('OR',  '10101010', '01010101', $res_or)  *
!*                                                                    *
!*      RESULTS: $res_xor = '01000001'                                *
!*               $res_and = '00000101'                                *
!*               $res_or  = '11111111'                                *
!*                                                                    *
!*               BIT Operation Properties (Bit Results)               *
!*               ==================================================== *
!*                                                                    *
!*               Operation:    (XOR)       (AND)       (OR)           *
!*                    Mask:   0 0 1 1     0 0 1 1     0 0 1 1         *
!*                  Object:   0 1 0 1     0 1 0 1     0 1 0 1         *
!*               ----------   -------     -------     -------         *
!*                  Result:   0 1 1 0     0 0 0 1     0 1 1 1         *
!*                                                                    *
!**********************************************************************

#ifndef BIT_Func_Remove

begin-procedure BIT-Func($I_op, $I_mask, $I_string, :$O_string)

uppercase $I_op

!   Synchronize Lengths (Mask/String)

let #len       = length($I_mask)
let #len2      = length($I_string)
if  #len       < #len2
    let #len   = #len2
end-if
let $I_mask    = lpad($I_mask,  #len,'0')
let $I_string  = lpad($I_string,#len,'0')

!   Perform Requested Operation

let $O_string  = ''
let #ctr       = 1

while #ctr    <= #len

   let $bit1   = substr($I_mask,  #ctr,1)
   let $bit2   = substr($I_string,#ctr,1)

   evaluate $I_op

      when = 'XOR'
         let $res     = '1'
         if  $bit1    = $bit2
             let $res = '0'
         end-if
         break

      when = 'OR'
         let $res     = '0'
         if  $bit1    = '1'
         or  $bit2    = '1'
             let $res = '1'
         end-if
         break

      when = 'AND'
         let $res     = $bit2
         if  $bit1    = '0'
             let $res = '0'
         end-if
         break

      when-other

         let $res     = ''

   end-evaluate

   let $O_string      = $O_string || $res

   let #ctr           = #ctr + 1

end-while

end-procedure

#endif

!**********************************************************************
                    

   Feedback
ftoct01.jpg (12389 bytes) I would appreciate any feedback you may have on this site. Send mail to tdelia@erols.com or click on the Octopus.

   Technical difficulties?
Zostrich.jpg (24850 bytes) Please report any technical difficulties you may encounter to the address above OR click on the Octopus. Thanks.

NOTE - Use your Browsers BACK Button to return to prior page.

Tony DeLia - Updated December 7, 2000