IBM Support

Hexadecimal Notation and Accessing Bits within a COBOL/400 Program

Troubleshooting


Problem

This document explains how to determine what bits are on/off for a field that holds a HEX value.

Resolving The Problem

Is there a way to determine what bits are on/off for a field that holds a HEX value with the current HEX support in COBOL/400? If not, what is the best way to determine.

Hex Notation and Access to Bits

The new Hex support allows you to specify a hex literal anywhere a nonnumeric literal was allowed. So you can use a hex literal in the VALUE clause of a PIC X data name, as in your example. You could also code an IF statement like:

IF X"C1" = FIELD-IN-FILE THEN

However, hex literal support does not let you set or test a particular bit within a PIC X field. It is possible in COBOL/400 to do it but it involves some tricky binary number/REDEFINE and arithmetic statements to accomplish it.

RPG lets you set bits on or off within a character string. Once this is done, you can then do character compares. To test a bit you would use the TESTB operation.

It is possible to determine bit status in COBOL, for example:
       05 BINARY-VAL PIC S9(3) BINARY VALUE ZERO.   
       05 FILLER REDEFINES BINARY-VAL.              
           10 FILLER PIC X.                         
           10 CHAR-VAL PIC X.                       
       05 BIT-VAL PIC 9.                            
       88 BIT-ON VALUE "1".                         
       88 BIT-OFF VALUE "0".                        
If you now MOVE your character into char-val then repeating the following DIVIDE will peel off each bit in turn and bit-on/bit-off will tell you the status of each.

DIVIDE binary-val BY 2 GIVING binary-val REMAINDER bit-val.

If you are interested only in the first bit of the low order nibble:

DIVIDE binary-val BY 8

before doing the DIV...REM operation.

Much faster (but longer to code) would be to set up a table with all the settings in place. For example:
       05 BIT-DATA USAGE DISPLAY.                                                  
           10 HEX-01 PIC 9(8) VALUE 00000001. <<< - LEADING ZEROES FOR CLARITY ONLY
           10 HEX-02 PIC 9(8) VALUE 00000010.                                      
           :                                                                       
           :                                                                       
          10 HEX-FE PIC 9(8) VALUE 11111110.                                       
          10 HEX-FF PIC 9(8) VALUE 11111111.                                       
                                                                                   
       05 FILLER REDEFINES BIT-DATA.                                               
          10 BIT-ARRAY OCCURS 256.                                                 
       15 BITS PIC 9 OCCURS 8 USAGE DISPLAY.                                       
       88 BIT-ON VALUE "1".                                                        
       88 BIT-OFF VALUE "0".                                                       

Now after a MOVE of your character into char-val, you can code the test you want, for example:
IF bit-on (binary-val + 1, 1) ..... Tests the high order bit.

[{"Type":"MASTER","Line of Business":{"code":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m3p000000F98bAAC","label":"Programming ILE Languages-\u003ECOBOL"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]

Historical Number

8854952

Document Information

Modified date:
09 December 2024

UID

nas8N1010099