Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to "read"/"write" bits in COBOL

Status
Not open for further replies.

slade

Programmer
Jun 11, 2000
823
US
This topic was brought up in another forum. Here's 2 possible approaches. Any other suggestion out there?

Regards, Jack.

The first will "flip" one specific bit in a specified byte. The other will scan 8 bytes of data and determine the "on/off" status of each bit in the string. In the code supplied here I've "flipped" each bit in the string. The routine can be used to selectively "flip" the bits, or, if the string represents an option list or some such, it can be used to process the options.

Because the code hasn't been tested, the usual caveats and copouts apply. I don't know how this will "wrap", but here goes.....

FLIPONE.....

Code:
01  WS-WORK-AREAS.
    05  BIT-NBR                 PIC  9(04) COMP.
    05  BIT-FLIPPER             PIC  9(04) COMP VALUE 0.

    05  FLD-1.
        10  1ST-CHK-BYTE        PIC  X(01).
            88  CHK-BIT-IS-ON   VALUE X'80' THRU X'FF'.
            88  CHK-BIT-IS-OFF  VALUE X'00' THRU X'7F'.
        10  FILLER              PIC  X(01).
    05  CHK-BITS                REDEFINES
        FLD-1                   PIC  9(04) COMP.

    05  CHG-BITS                PIC  9(04) COMP.

    05  BIT-SHIFT-VALUES.
        10  BIT-1               PIC  9(04) COMP VALUE   1.
        10  BIT-2               PIC  9(04) COMP VALUE   2.
        10  BIT-3               PIC  9(04) COMP VALUE   4.
        10  BIT-4               PIC  9(04) COMP VALUE   8.
        10  BIT-5               PIC  9(04) COMP VALUE  16.
        10  BIT-6               PIC  9(04) COMP VALUE  32.
        10  BIT-7               PIC  9(04) COMP VALUE  64.
        10  BIT-8               PIC  9(04) COMP VALUE 128.
    05  BIT-SHIFT-TBL           REDEFINES
        BIT-SHIFT-VALUES.
        10  SHIFT-ENT           PIC  9(04) COMP
                                OCCURS 8.


    MOVE     your data  TO CHK-BITS(1:1)
                           CHG-BITS(1:1)
    MOVE     bit nbr    TO BIT-NBR
    MOVE     ZEROS      TO BIT-FLIPPER
    MOVE     X'80'      TO BIT-FLIPPER(1:1)
    COMPUTE  CHK-BITS    = CHK-BITS    * SHIFT-ENT(BIT-NBR)
    COMPUTE  BIT-FLIPPER = BIT-FLIPPER / SHIFT-ENT(BIT-NBR)

    EVALUATE TRUE
    WHEN CHK-BIT-IS-ON
         SUBTRACT BIT-FLIPPER FROM CHG-BITS
    WHEN CHK-BIT-IS-OFF
         ADD BIT-FLIPPER        TO CHG-BITS
    WHEN OTHER
         DISPLAY 'HOUSTON WE HAVE A PROBLEM'
         STOP RUN
    END-EVALUATE 
    MOVE CHG-BITS(1:1)  TO your receiving area
    .
FLIPALL.....

Code:
01  WS-WORK-AREAS.
    05  FLD-1.
        10  1ST-CHK-BYTE        PIC  X(01).
            88  CHK-BIT-IS-ON   VALUE X'80' THRU X'FF'.
            88  CHK-BIT-IS-OFF  VALUE X'00' THRU X'7F'.
        10  FILLER              PIC  X(07).
    05  CHK-BITS-64             REDEFINES
        FLD-1                   PIC  9(10) COMP.

    05  CHG-BITS-64             PIC  9(10) COMP.
    05  BIT-FLIPPER             PIC  9(10) COMP VALUE ZEROS.
    05  LOOP-CNTL               PIC  9(04) COMP VALUE ZEROS.
    05  BIT-LOC                 PIC  9(04) COMP VALUE ZEROS.
        88  ONE-BIT             VALUE 1 THRU 8.
        88  BIT-STRING          VALUE 0.
        88  ERROR               VALUE 9 THRU 65535.



    MOVE     your data    TO CHK-BITS-64(1:nbr of bytes)
                             CHG-BITS-64(1:nbr of bytes)
    MOVE     nbr of bits  TO LOOP-CNTL
    MOVE     ZEROS        TO BIT-FLIPPER
    MOVE     X'80'        TO BIT-FLIPPER(1:1)

    PERFORM  XXXX-CHK-A-BIT  LOOP-CNTL TIMES
    MOVE CHG-BITS-64(1:nbr of bytes)
      TO your receiving area
    .
XXXX-CHK-A-BIT.
    EVALUATE TRUE
    WHEN CHK-BIT-IS-ON
         SUBTRACT BIT-FLIPPER FROM CHG-BITS-64
    WHEN CHK-BIT-IS-OFF
         ADD BIT-FLIPPER        TO CHG-BITS-64
    WHEN OTHER
         DISPLAY 'HOUSTON WE HAVE A PROBLEM'
         STOP RUN
    END-EVALUATE
    COMPUTE  CHK-BITS-64 = CHK-BITS-64          * 2
    COMPUTE  BIT-FLIPPER = BIT-FLIPPER          / 2
    .


 
Many manufacturers provide a variety of extensions to handle bit manipulations. On the IBM mainframe, Language Environment (LE) provides callable services to do bit manipulation (e.g. CEESISET).

Regards,

Glenn
 
Yeah, but that's cheatin'. That aint no cobol solution. :)

Regards, Jack.
 
I agree with 3gm.
I did it several years ago on VAX/VMS using
macro assembly instructions to set/mask/read etc bits in
a whole 32bit word.
I've used bits to save a disk space in a large data
file. In 4 bytes I used:
for date of payment: 4bits for year (+1990 assumed ...
workable until 2006); 4bits for month; 5bits for day;
example: 21-May-1996 => 0110 0101 10101
for type of subscriber: 2bits;
for place of payment (bank,post...): 3bits;
for month paying for: 4bits;
for several other flags: 6 bits;
I had even a "spare" bits!
Of course it was not intended to be searchable
(indexable) but it was a great space-saving job.
Anyway that was much faster and with smaller code
than using a pure cobol.

Regards
Georgi
 
Hi
Here rutine that compress &/or decomress 8 bytes of 0,1 to
a 1 byte that his value in range in hexa. 00-FF.

000001 IDENTIFICATION DIVISION.
000002 PROGRAM-ID. BITCOMP.
000003 DATA DIVISION.
000004 WORKING-STORAGE SECTION.
000005 01 I PIC 9(4) COMP-6.
000006 01 PORT PIC 9(4) COMP-6.
000007 01 REMA PIC 9(4) COMP-6.
000008 01 N-X.
000009 02 N-1-4 PIC 9999.
000010 02 N-5-8 PIC 9999.
000011 01 N REDEFINES N-X PIC 9(08).
000012 01 XX PIC XX VALUE LOW-VALUES.
000013 01 XX-R REDEFINES XX.
000014 02 FILLER PIC X.
000015 02 X PIC X.
000016 01 X-9 REDEFINES XX PIC 9(4) COMP-4.
000017 01 VECTOR-BIN PIC X(132)
000018 VALUE "00000001001000110100010101100111100010011010101111@A01
000019- "00110111101111 @B
000020- " ". @C
000021 01 VECTOR-BIN-R REDEFINES VECTOR-BIN.
000022 02 VEC-BIN PIC 9(4) OCCURS 16.
000023 01 VECTOR-HEX PIC X(132)
000024 VALUE "00000001000200030004000500060007000800090010001100@A02
000025- "1200130014001500000016003200480064008000960112012801@B
000026- "44016001760192020802240240 ". @C
000027 01 VECTOR-HEX-R REDEFINES VECTOR-HEX.
000028 02 VEC-HEX PIC 9(4) OCCURS 32.
000029 LINKAGE SECTION.
000030* SW=1 COMPRESS , SW=2 UNCOMPRESS
000031* COMP-FIELD MUST BE STRING OF 0,1.
000032 01 SW PIC 9.
000033 01 COMP-FIELD PIC 9(8).
000034 01 BIT-FIELD-X.
000035 02 BIT-FIELD PIC 9.
000036 PROCEDURE DIVISION USING SW COMP-FIELD BIT-FIELD-X.
000037 MAIN SECTION.
000038 1. IF SW = 1 MOVE COMP-FIELD TO N
000039 PERFORM IN-COMPRESS
000040 MOVE X TO BIT-FIELD-X
000041 ELSE IF SW = 2 MOVE BIT-FIELD-X TO X
000042 PERFORM OUT-COMPRESS
000043 MOVE N TO COMP-FIELD.
000044 EXIT-PROGRAM.
000045 EXIT PROGRAM.
000046 IN-COMPRESS.
000047 PERFORM DEC-TO-BIN1 VARYING I FROM 1 BY 1 UNTIL I > 16.
000048 IF I < 77 DISPLAY &quot;ERROR-1 IN BITCOMP&quot;
000049 DISPLAY N-1-4
000050 CALL &quot;cancel&quot;.
000051 PERFORM DEC-TO-BIN2 VARYING I FROM 1 BY 1 UNTIL I > 16.
000052 IF I < 77 DISPLAY &quot;ERROR-2 IN BITCOMP&quot;
000053 DISPLAY N-5-8
000054 CALL &quot;cancel&quot;.
000055 DEC-TO-BIN1.
000056 IF N-1-4 = VEC-BIN(I) ADD 16 TO I
000057 MOVE VEC-HEX(I) TO X-9
000058 MOVE 77 TO I.
000059 DEC-TO-BIN2.
000060 IF N-5-8 = VEC-BIN(I) ADD VEC-HEX(I) TO X-9
000061 MOVE 77 TO I.
000062 EX-IN-COMPRESS.
000063 OUT-COMPRESS.
000064 DIVIDE X-9 BY 16 GIVING PORT REMAINDER REMA.
000065 ADD 1 TO PORT REMA.
000066 MOVE VEC-BIN(PORT) TO N-1-4.
000067 MOVE VEC-BIN(REMA) TO N-5-8.
000068 EX-OUT-COMPRESS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top