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.....
FLIPALL.....
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
.
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
.