![]() |
COBOL Bit Processing Bit Manipulation |
| When technology complements business | Copyright © 1987-2012 SimoTime Enterprises All Rights Reserved |
| The SimoTime Home Page |
This is an example of how COBOL can do bit-level manipulation. The three basic functions provided are as follows.
| ||||||
| The Three Basic Functions provided by the Bit Manipulation Routine |
This example uses an approach of converting the bit information in a single byte to or from an eight-byte field of COBOL accessible zeroes and ones. This example contains one JCL member to execute the two COBOL programs. The main COBOL program (CBLBITC1.CBL) is used to demonstrate and test the bit manipulation functions. The bit manipulation functions are in a separate, callable COBOL program (SIMOBITS.CBL). Both COBOL programs were written and tested using the VS COBOL II dialect. Also, both COBOL programs will work with COBOL for MVS and COBOL/370. A JCL member is provided to run the job as an MVS batch job on an IBM mainframe or as a project with Micro Focus Mainframe Express (MFE) running on a PC with Windows. Also, a CMD member is provided to run the job with Micro Focus Net Express (MFE) running on a PC with Windows.
In the world of programming there are many ways to solve a problem. This suite of programs is provided as a COBOL programming example of one of the possible solutions to the problem of determining and changing the bits within a byte. This example may serve as a tutorial for new programmers and as a reference for experienced programmers. Additional information (including the original Assembler routine that does bit manipulation) is provided in the Downloads and Links to Similar Pages section of this document.
This type of bit manipulation is quite often considered something that cannot be done using COBOL. Many years ago I was asked to provide a mainframe assembler routine to access the bits within a byte. I delivered the callable routine and was well paid for the task. However, something the client said bothered me, "I have been told this cannot be done in COBOL."
I have heard this statement many times over the years and in a number of instances it is simply not true. An assembler routine would probably be smaller and run faster. However, if the task could be accomplished in a reasonable manner using COBOL then the cost, effort, frustration and ongoing support of an assembler routine in a COBOL programming shop may not be worth these advantages. This is especially true if assembler expertise is not readily available.
Determining and changing the setting of a bit is possible using COBOL. An assembler routine or the use of a language extension is not required. The called COBOL routine (SIMOBITS) in this example provides two functions.
Note: Refer to the Download and Links to Similar Pages section of this document for links to additional documents that discuss this topic.
For each bit that is ON (1) in the BTS-PASS-BITS field the corresponding byte in the BTS-PASS-BYTES field is set to a value of one. For each bit that is OFF (0) in the BTS-PASS-BITS field the corresponding byte in the BTS-PASS-BYTES field is set to a value of zero.
| ||||||
| Expand Function of the Bit manipulation Routine |
For each byte that is a one in the BTS-PASS-BYTES field the corresponding bit in the BTS-PASS-BITS field is set to ON (1). For each byte that is zero in the BTS-PASS-BYTES field the corresponding bit in the BTS-PASS-BITS field is set to OFF (0).
| ||||||
| Compress Function of the Bit manipulation Routine |
The callable interface requires a data structure to be defined. A copy file (PASSBITS.CPY) is provided with the following source code.
*****************************************************************
* PASSBITS is a COBOL Copy File *
* Data Structure or Pass Area used for calling SIMOBITS. *
* Copyright (C) 1987-2012 SimoTime Enterprises *
* All Rights Reserved *
*****************************************************************
* Provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
*
* When BTS-PASS-REQUEST is 'EXPAND ' then
* BTS-PASS-BITS (a 1-btye field of 8-bits) is analyzed
* and based on the bit settings a corresponding value of
* 0 or 1 is set in the BTS-PASS-BYTES (an 8-byte field
* of zeroes and ones).
*
* When BTS-PASS-REQUEST is 'COMPRESS' then
* BTS-PASS-BYTES (an 8-byte field of zeroes and ones) is
* analyzed and based on a value of 0 or 1 the corresponding
* bit in BTS-PASS-BITS (a 1-byte field of 8-bits) is
* switched ON or OFF.
*
01 BTS-PASS-AREA.
05 BTS-PASS-REQUEST PIC X(8).
05 BTS-PASS-RESULT PIC S9(9) COMP.
05 BTS-PASS-RECORD.
10 BTS-PASS-BITS PIC X.
10 BTS-PASS-BYTES.
15 BTS-PASS-BYTE-01 PIC X.
15 BTS-PASS-BYTE-02 PIC X.
15 BTS-PASS-BYTE-03 PIC X.
15 BTS-PASS-BYTE-04 PIC X.
15 BTS-PASS-BYTE-05 PIC X.
15 BTS-PASS-BYTE-06 PIC X.
15 BTS-PASS-BYTE-07 PIC X.
15 BTS-PASS-BYTE-08 PIC X.
*
*** PASSBITS - End-of-Copy File - - - - - - - - - - - PASSBITS *
*****************************************************************
*
The following will translate an eight character field of 0's and 1's to a one byte field with the bits set to match to 0's and 1's of the eight character field. Please note: the content of the BTS-PASS-REQUEST field must be upper case and contain 'COMPRESS'
*****************************************************************
* The coding required to do the call to the Byte-Bit Routine *
* (translate an 8-byte field to a 1-byte field). *
*****************************************************************
MOVE '11111111' to BTS-PASS-BYTES
MOVE 'COMPRESS' to BTS-PASS-REQUEST
CALL 'SIMOBITS' using BTS-PASS-AREA
In the preceding example the contents of BTS-PASS-BITS will be X'FF' or high-value upon return from the call to SIMOBITS.
The following will set the individual bytes of an eight character field to 0's or 1's based on the bit settings of a one byte field. Please note: the content of the BTS-PASS-REQUEST field must be upper case and contain 'EXPAND' followed by two spaces
*****************************************************************
* The coding required to do the call to the Byte-Bit Routine *
* (translate a 1-byte field to an 8-byte field). *
*****************************************************************
MOVE X'55' to BTS-PASS-BITS
MOVE 'EXPAND ' to BTS-PASS-REQUEST
CALL 'SIMOBITS' using BTS-PASS-AREA
In the preceding example the contents of BTS-PASS-BYTES will be '01010101' upon return from the call to SIMOBITS.
The following is the Windows Command file (CBLBITE1.CMD) that is required to run as a job on a PC using Micro Focus Net Express.
@echo OFF
rem * *******************************************************************
rem * CBLBITE1.CMD - a Windows Command File *
rem * This program is provided by SimoTime Enterprises *
rem * (C) Copyright 1987-2012 All Rights Reserved *
rem * Web Site URL: http://www.simotime.com *
rem * e-mail: helpdesk@simotime.com *
rem * *******************************************************************
rem *
rem * Text - COBOL and Bit Manipulation
rem * Author - SimoTime Enterprises
rem * Date - November 11, 2003
rem * Version - 06.07.16
rem *
rem * This set of programs illustrate the use a COBOL program to do
rem * bit manipulation. This suite of programs will convert between
rem * a one-byte (eight-bits) field and an eight-byte field.
rem *
rem * When running with Net Express the IBMCOMP an NOTRUNC directives
rem * will be required to maintain compatability with the mainframe
rem * format and field sizes for binary fields.
rem *
rem * This technique provides for the use of a single COBOL source
rem * program that will run on OS/390, Windows or Unix.
rem *
rem * This set of programs will run on a Personal Computer with Windows
rem * and Micro Focus Net Express.
rem *
rem * ************
rem * * CBLBITE1 *
rem * ********cmd*
rem * *
rem * ************ ************
rem * * call ******* Env1BASE *
rem * ************ ********cmd*
rem * *
rem * *
rem * ************ ************ ************
rem * * run ******* CBLBITC1 ******* SYSOUT *
rem * ************ ********cbl* ************
rem * * *
rem * * *
rem * * ************
rem * * * SIMOBITS *
rem * * ********cbl*
rem * ************
rem * * EOJ *
rem * ************
rem *
rem * ********************************************************************
rem * Step 1 of 2 Set the global environment variables...
rem *
setlocal
set CmdName=CblBitE1
call ..\Env1BASE %CmdName%
if "%SYSLOG%" == "" set syslog=c:\SimoLIBR\LOGS\SimoTime.LOG
rem *
call SimoNOTE "*******************************************************CblBitE1"
call SimoNOTE "Starting CmdName %CmdName%, V08.06.05, User is %USERNAME%"
rem * ********************************************************************
rem * Step 2 of 2 Execute the sample program...
rem *
run CblBitC1
if not "%ERRORLEVEL%" == "0" set JobStatus=0010
if not "%JobStatus%" == "0000" goto :EojNOK
:EojAOK
call SimoNOTE "Finished CmdName %CmdName%, Job Status is %JobStatus% "
goto :End
:EojNOK
call SimoNOTE "ABENDING CmdName %CmdName%, Job Status is %JobStatus% "
goto :End
:End
call SimoNOTE "Conclude SYSOUT is %SYSOUT%"
if not "%1" == "nopause" pause
endlocal
The following is the mainframe JCL member (CBLBITJ1.JCL) that is required to run as a job on the mainframe or as a Mainframe Express project on the PC.
//CBLBITJ1 JOB SIMOTIME,'CBL BIT MANIPULATE',CLASS=1,MSGCLASS=0, // NOTIFY=CSIP1 //* ******************************************************************* //* This JCL member is provided by SimoTime Enterprises * //* (C) Copyright 1987-2012 All Rights Reserved * //* Web Site URL: http://www.simotime.com * //* e-mail: helpdesk@simotime.com * //* ******************************************************************* //* //* Text - COBOL calls COBOL for Bit and Byte manipulation //* Author - SimoTime Enterprises //* Date - January 01, 1989 //* //* This set of programs illustrate the use a COBOL program to do //* bit manipulation. This suite of programs will convert between //* a one-byte (eight-bits) field and an eight-byte field. //* //* The DD statement for SYSOUT is required by this example. The //* COBOL program may be compiled using the SYSOUT directive and //* without the DD statement for SYSOUT an RTS173 or RTS227 may occur. //* //* This set of programs will run on a mainframe under MVS or on //* a Personal Computer running Windows and Mainframe Express by //* Micro Focus. //* //* ************ //* * CBLBITJ1 * //* ********jcl* //* * //* * //* ************ ************ //* * CBLBITC1 *-----* DISPLAY * //* ********cbl* ************ //* * * //* * * ************ //* * *-CALL--* SIMOBITS * //* * ********cbl* //* * //* ************ //* * EOJ * //* ************ //* //* ******************************************************************* //* Step 1 of 1, This is a single step job. //* //CBLBITS1 EXEC PGM=CBLBITC1 //STEPLIB DD DSN=SIMOTIME.DEMO.LOADLIB1,DISP=SHR //SYSOUT DD SYSOUT=* //*
This program (CBLBITC1.CBL) was written to test the callable COBOL program (SIMOBITS.CBL) that does the conversion between a 1-byte field of 8-bits and a field of 8-bytes.
IDENTIFICATION DIVISION.
PROGRAM-ID. CBLBITC1.
AUTHOR. SIMOTIME ENTERPRISES.
*****************************************************************
* Copyright (C) 1987-2012 SimoTime Enterprises. *
* *
* All rights reserved. Unpublished, all rights reserved under *
* copyright law and international treaty. Use of a copyright *
* notice is precautionary only and does not imply publication *
* or disclosure. *
* *
* Permission to use, copy, modify and distribute this software *
* for any non-commercial purpose and without fee is hereby *
* granted, provided the SimoTime copyright notice appear on all *
* copies of the software. The SimoTime name or Logo may not be *
* used in any advertising or publicity pertaining to the use *
* of the software without the written permission of SimoTime *
* Enterprises. *
* *
* Permission to use, copy, modify and distribute this software *
* for any commercial purpose requires a fee to be paid to *
* SimoTime Enterprises. Once the fee is received by SimoTime *
* the latest version of the software will be delivered and a *
* license will be granted for use within an enterprise, *
* provided the SimoTime copyright notice appear on all copies *
* of the software. The SimoTime name or Logo may not be used *
* in any advertising or publicity pertaining to the use of the *
* software without the written permission of SimoTime *
* Enterprises. *
* *
* SimoTime Enterprises makes no warranty or representations *
* about the suitability of the software for any purpose. It is *
* provided "AS IS" without any expressed or implied warranty, *
* including the implied warranties of merchantability, fitness *
* for a particular purpose and non-infringement. SimoTime *
* Enterprises shall not be liable for any direct, indirect, *
* special or consequential damages resulting from the loss of *
* use, data or projects, whether in an action of contract or *
* tort, arising out of or in connection with the use or *
* performance of this software *
* *
* SimoTime Enterprises *
* 15 Carnoustie Drive *
* Novato, CA 94949-5849 *
* 415.883.6565 *
* *
* RESTRICTED RIGHTS LEGEND *
* Use, duplication, or disclosure by the Government is subject *
* to restrictions as set forth in subparagraph (c)(1)(ii) of *
* the Rights in Technical Data and Computer Software clause at *
* DFARS 52.227-7013 or subparagraphs (c)(1) and (2) of *
* Commercial Computer Software - Restricted Rights at 48 *
* CFR 52.227-19, as applicable. Contact SimoTime Enterprises, *
* 15 Carnoustie Drive, Novato, CA 94949-5849. *
* *
*****************************************************************
* This program is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
*
*****************************************************************
* Source Member: CBLBITC1.CBL
* Copy Files: PASSBITS.CPY
* Calls to: SIMOBITS
*****************************************************************
*
* CBLBITC1 - Call SIMOBITS to convert between bits and bytes.
*
* CALLING PROTOCOL
* ----------------
* Use standard procedure to RUN or ANIMATE.
*
* DESCRIPTION
* -----------
* This program will call a COBOL routine to access a
* routine to convert between bits and bytes..
*
* ************
* * CBLBITJ1 *
* ********jcl*
* *
* *
* ************ ************
* * CBLBITC1 *-----* CONSOLE *
* ********cbl* ******dsply*
* *
* *
* ************
* * SIMOBITS *
* ********cbl*
*
* This program calls SIMOBITS to perform two functions:
*
* EXPAND - translate the bits of a one-byte field to bytes
* in an eight-byte field.
* COMPRESS - translate the bytes of an eight-byte field into
* bits in a one-byte field.
*
*****************************************************************
* The EXPAND function will do the following:
*
* Input: BTS-PASS-BITS, a one byte field (8-bits)
* OUTPUT: BTS-PASS-BYTES, an eight byte field
*
* For each bit that is on in the BTS-PASS-BITS field set the
* corresponding byte in the BTS-PASS-BYTES field to a value
* of one.
*
* For each bit that is off in the BTS-PASS-BITS field set the
* corresponding byte in the BTS-PASS-
* BYTES field to a value of zero.
*
* Example: if BTS-PASS-BITS = x'55'
* then BTS-PASS-BYTES will be '01010101'
*
*****************************************************************
* The COMPRESS function will do the following:
*
* Input: BTS-PASS-BYTES, an eight byte field
* Output: BTS-PASS-BITS, a one byte field (8-bits)
*
* For each byte that is a one in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to ON (1)
*
* For each byte that is zero in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to OFF (0).
*
* Example: if BTS-PASS-BYTES = '01010101'
* then BTS-PASS-BITS set to x'55'
*
*****************************************************************
*
* MAINTENANCE
* -----------
* 1989/02/27 Simmons, Created program.
* 1997/03/17 Simmons, Updated for call to SIMOBITS.
*
*****************************************************************
*
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
*****************************************************************
* Data-structure for Title and Copyright...
* ------------------------------------------------------------
01 SIM-TITLE.
05 T1 pic X(11) value '* CBLBITC1 '.
05 T2 pic X(34) value 'Convert between Bits and Bytes '.
05 T3 pic X(10) value ' v08.06.05'.
05 T4 pic X(24) value ' http://www.simotime.com'.
01 SIM-COPYRIGHT.
05 C1 pic X(11) value '* CBLBITC1 '.
05 C2 pic X(20) value 'Copyright 1987-2012 '.
05 C3 pic X(28) value '--- SimoTime Enterprises ---'.
05 C4 pic X(20) value ' All Rights Reserved'.
01 SIM-THANKS-01.
05 C1 pic X(11) value '* CBLBITC1 '.
05 C2 pic X(32) value 'Thank you for using this softwar'.
05 C3 pic X(32) value 'e provided from SimoTime Enterpr'.
05 C4 pic X(04) value 'ises'.
01 SIM-THANKS-02.
05 C1 pic X(11) value '* CBLBITC1 '.
05 C2 pic X(32) value 'Please send all inquires or sugg'.
05 C3 pic X(32) value 'estions to the helpdesk@simotime'.
05 C4 pic X(04) value '.com'.
COPY PASSBITS.
*****************************************************************
* BTS-PASS-REQUEST values when calling SIMOBITS.
* ------------------------------------------------------------
01 REQUEST-4-EXPAND pic X(8) value 'EXPAND '.
01 REQUEST-4-COMPRESS pic X(8) value 'COMPRESS'.
*****************************************************************
* Buffer used for posting messages to the console.
* ------------------------------------------------------------
01 MESSAGE-BUFFER.
05 MESSAGE-HEADER pic X(11) value '* CBLBITC1 '.
05 MESSAGE-TEXT pic X(68).
*****************************************************************
* Work fields used for testing call to SIMOBITS.
* ------------------------------------------------------------
01 HEX-00 pic X.
01 HEX-55 pic X.
01 HEX-AA pic X.
01 HEX-FF pic X.
01 BYTES-00 pic X(8).
01 BYTES-55 pic X(8).
01 BYTES-AA pic X(8).
01 BYTES-FF pic X(8).
01 ZERO-VALUE pic 9 value 0.
01 MINUS-ONE pic S9 value -1.
01 MINUS-ONE-X redefines MINUS-ONE
pic X.
01 POSITIVE-BIT-VALUE pic X(8) value '00000000'.
01 NEGATIVE-BIT-VALUE pic X(8) value '00000000'.
01 THREE-BYTES.
05 PACK-03 pic S9(5) comp-3 value 0.
01 TWO-BYTES pic X(2) value '00'.
01 EIGHT-BYTES pic X(8) value LOW-VALUES.
01 THE-EASY-WAY.
05 FILLER pic X(9) value 'Efficient'.
05 FILLER pic X(10) value ', PACK-03 '.
05 PACK-03-E1L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-E1R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-E2L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-E2R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-E3L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-E3R pic X(4) value SPACES.
05 FILLER pic X(13) value ', UNPACKED-5='.
05 UNPACKED-5 pic 9(5) value 0.
01 THE-HARD-WAY.
05 FILLER pic X(9) value 'Difficult'.
05 FILLER pic X(10) value ', PACK-03 '.
05 PACK-03-H1L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-H1R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-H2L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-H2R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-H3L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-H3R pic X(4) value SPACES.
05 FILLER pic X(13) value ', FIVE-BYTES='.
05 FIVE-BYTES pic X(5) value '00000'.
01 ALPHABET-UPPER pic X(26) value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
01 IX-1 pic 9(3) value 0.
01 IX-2 pic 9(3) value 0.
01 IX-3 pic 9(3) value 0.
*****************************************************************
PROCEDURE DIVISION.
perform Z-POST-COPYRIGHT
perform BYTES-TO-BITS-COMPRESS.
perform BITS-TO-BYTES-EXPAND.
perform ALPHABET-DUMP.
perform COBOL-UNPACK.
perform Z-THANK-YOU.
move ZERO to RETURN-CODE
GOBACK.
*****************************************************************
ALPHABET-DUMP.
move 'Starting ALPHABET-DUMP Routine...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
add 1 to ZERO giving IX-1
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
perform 26 times
move ALPHABET-UPPER(IX-1:1) to BTS-PASS-BITS
call 'SIMOBITS' using BTS-PASS-AREA
move 'Position nnn is x, the binary value is xxxx-xxxx'
to MESSAGE-TEXT
move IX-1 to MESSAGE-TEXT(10:03)
move BTS-PASS-BITS to MESSAGE-TEXT(17:1)
move BTS-PASS-BYTES(1:4) to MESSAGE-TEXT(40:4)
move BTS-PASS-BYTES(5:4) to MESSAGE-TEXT(45:4)
perform Z-POST-MESSAGE
add 1 to IX-1
end-perform
exit.
*****************************************************************
BITS-TO-BYTES-EXPAND.
move 'Starting BITS-TO-BYTES-EXPAND Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
move HEX-00 to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-00
perform DISPLAY-BYTES
move HEX-FF to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-FF
perform DISPLAY-BYTES
move HEX-55 to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-55
perform DISPLAY-BYTES
move HEX-AA to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-AA
perform DISPLAY-BYTES
exit.
*****************************************************************
BYTES-TO-BITS-COMPRESS.
move 'Starting BYTES-TO-BITS-COMPRESS Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
move '00000000' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-00
perform DISPLAY-BYTES
move '11111111' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-FF
perform DISPLAY-BYTES
move '01010101' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-55
perform DISPLAY-BYTES
move '10101010' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-AA
perform DISPLAY-BYTES
exit.
*****************************************************************
* If you thought the UnPack was reserved for the mainframe *
* assembler programmers then take a look at the following *
* routine. The is a bit of an esoteric use of bit mainipulation *
* but it does unpack a data string into a new data string. *
* *
* A quicker and better way to unpack a field is as follows: *
* ADD PACKED-FIELD TO ZERO GIVING UNPACKED-FIELD. *
*****************************************************************
COBOL-UNPACK.
move 'Starting the COBOL-UNPACK Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
add 615 to ZERO giving PACK-03
* Do the UNPACK the easy way...
add PACK-03 to ZERO giving UNPACKED-5
* First, determine the bit configuration for zero to be
* used to set the left-nibble of the unpacked bytes.
* Then determine the negative sign bit configuration.
* This section of code is used so the program will run
* properly in both an EBCDIC and ASCII environment.
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move ZERO-VALUE to BTS-PASS-BITS
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to POSITIVE-BIT-VALUE
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move MINUS-ONE-X to BTS-PASS-BITS
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to NEGATIVE-BIT-VALUE
move '0000' to NEGATIVE-BIT-VALUE(5:4)
* Do the UNPACK the esoteric way...
move all '0' to FIVE-BYTES
add 1 to ZERO giving IX-1
add 1 to ZERO giving IX-2
add 3 to ZERO giving IX-3
perform until IX-3 = 1
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move THREE-BYTES(IX-1:1) to BTS-PASS-BITS
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to EIGHT-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
move POSITIVE-BIT-VALUE to BTS-PASS-BYTES
move EIGHT-BYTES(1:4) to BTS-PASS-BYTES(5:4)
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BITS to FIVE-BYTES(IX-2:1)
add 1 to IX-2
move EIGHT-BYTES(5:4) to BTS-PASS-BYTES(5:4)
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BITS to FIVE-BYTES(IX-2:1)
subtract 1 from IX-3
add 1 to IX-2
add 1 to IX-1
end-perform
perform COBOL-UNPACK-UNITS.
* Display the results...
perform COBOL-UNPACK-POST-EFFICIENT
perform COBOL-UNPACK-POST-DIFFICULT
move 'Finished the COBOL-UNPACK Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*---------------------------------------------------------------*
COBOL-UNPACK-POST-DIFFICULT.
move THREE-BYTES(1:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-H1L
move BTS-PASS-BYTES(5:4) to PACK-03-H1R
move THREE-BYTES(2:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-H2L
move BTS-PASS-BYTES(5:4) to PACK-03-H2R
move THREE-BYTES(3:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-H3L
move BTS-PASS-BYTES(5:4) to PACK-03-H3R
move THE-HARD-WAY to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*---------------------------------------------------------------*
COBOL-UNPACK-POST-EFFICIENT.
move THREE-BYTES(1:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-E1L
move BTS-PASS-BYTES(5:4) to PACK-03-E1R
move THREE-BYTES(2:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-E2L
move BTS-PASS-BYTES(5:4) to PACK-03-E2R
move THREE-BYTES(3:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-E3L
move BTS-PASS-BYTES(5:4) to PACK-03-E3R
move THE-EASY-WAY to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*---------------------------------------------------------------*
COBOL-UNPACK-UNITS.
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move THREE-BYTES(IX-1:1) to BTS-PASS-BITS
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to EIGHT-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
if PACK-03 < 0
move NEGATIVE-BIT-VALUE to BTS-PASS-BYTES
else
move POSITIVE-BIT-VALUE to BTS-PASS-BYTES
end-if
move EIGHT-BYTES(1:4) to BTS-PASS-BYTES(5:4)
* move EIGHT-BYTES(5:4) to BTS-PASS-BYTES(1:4)
call 'SIMOBITS' using BTS-PASS-AREA
move BTS-PASS-BITS to FIVE-BYTES(IX-2:1)
exit.
*****************************************************************
DISPLAY-BYTES.
move 'Binary value is ' to MESSAGE-TEXT(1:16)
move BTS-PASS-BYTES(1:4) to MESSAGE-TEXT(17:4)
move '-' to MESSAGE-TEXT(21:1)
move BTS-PASS-BYTES(5:4) to MESSAGE-TEXT(22:4)
perform Z-POST-MESSAGE
exit.
*****************************************************************
* The following Z-Routines perform administrative tasks *
* for this program. *
*****************************************************************
*****************************************************************
Z-POST-COPYRIGHT.
display SIM-TITLE
display SIM-COPYRIGHT
exit.
*****************************************************************
Z-POST-MESSAGE.
display MESSAGE-BUFFER
move SPACES to MESSAGE-TEXT
exit.
*****************************************************************
Z-THANK-YOU.
display SIM-THANKS-01
display SIM-THANKS-02
exit.
*****************************************************************
* This example is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
This program (SIMOBITS.CBL) was originally written to be used as a teaching and learning aid. It was first used in a production environment to replace a callable 370 assembler routine. Today, this program is used in a number of mainframe shops and it is the program of choice used by SimoTime. Also, the program has been tested and deployed on the PC with Mainframe Express or Net Express from Micro Focus.
IDENTIFICATION DIVISION.
PROGRAM-ID. SIMOBITS.
AUTHOR. SIMOTIME ENTERPRISES.
*****************************************************************
* Copyright (C) 1987-2012 SimoTime Enterprises. *
* *
* All rights reserved. Unpublished, all rights reserved under *
* copyright law and international treaty. Use of a copyright *
* notice is precautionary only and does not imply publication *
* or disclosure. *
* *
* Permission to use, copy, modify and distribute this software *
* for any commercial purpose requires a fee to be paid to *
* SimoTime Enterprises. Once the fee is received by SimoTime *
* the latest version of the software will be delivered and a *
* license will be granted for use within an enterprise, *
* provided the SimoTime copyright notice appear on all copies *
* of the software. The SimoTime name or Logo may not be used *
* in any advertising or publicity pertaining to the use of the *
* software without the written permission of SimoTime *
* Enterprises. *
* *
* Permission to use, copy and modify this software for any *
* non-commercial purpose and without fee is hereby granted, *
* provided the SimoTime copyright notice appear on all copies *
* of the software. The SimoTime name or Logo may not be used *
* in any advertising or publicity pertaining to the use of the *
* software without the written permission of SimoTime *
* Enterprises. *
* *
* SimoTime Enterprises makes no warranty or representations *
* about the suitability of the software for any purpose. It is *
* provided "AS IS" without any expressed or implied warranty, *
* including the implied warranties of merchantability, fitness *
* for a particular purpose and non-infringement. SimoTime *
* Enterprises shall not be liable for any direct, indirect, *
* special or consequential damages resulting from the loss of *
* use, data or projects, whether in an action of contract or *
* tort, arising out of or in connection with the use or *
* performance of this software *
* *
* SimoTime Enterprises *
* 15 Carnoustie Drive *
* Novato, CA 94949-5849 *
* 415.883.6565 *
* *
* RESTRICTED RIGHTS LEGEND *
* Use, duplication, or disclosure by the Government is subject *
* to restrictions as set forth in subparagraph (c)(1)(ii) of *
* the Rights in Technical Data and Computer Software clause at *
* DFARS 52.227-7013 or subparagraphs (c)(1) and (2) of *
* Commercial Computer Software - Restricted Rights at 48 *
* CFR 52.227-19, as applicable. Contact SimoTime Enterprises, *
* 15 Carnoustie Drive, Novato, CA 94949-5849. *
* *
*****************************************************************
* This program is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
* *
*****************************************************************
*
*****************************************************************
* Source Member: SIMOBITS.CBL
* Copy Files: PASSBITS.CPY
*****************************************************************
*
* SIMOBITS - A called routine for bit and byte conversions.
*
* CALLING PROTOCOL
* ----------------
* Use standard COBOL calling procedures.
*
* DESCRIPTION
* -----------
* This program will do Bit and Byte conversions.
*
****************************************************************
*
* MAINTENANCE
* -----------
* 1989/02/27 Simmons, Created program.
* 1989/02/27 Simmons, no changes to date
*
*****************************************************************
*
DATA DIVISION.
WORKING-STORAGE SECTION.
*
*****************************************************************
* Data-structure for Title and Copyright...
* ------------------------------------------------------------
01 SIM-TITLE.
05 T1 pic X(11) value '* SIMOBITS '.
05 T2 pic X(34) value 'Convert between Bits and Bytes '.
05 T3 pic X(10) value ' v06.11.14'.
05 T4 pic X(24) value ' http://www.simotime.com'.
01 SIM-COPYRIGHT.
05 C1 pic X(11) value '* SIMOBITS '.
05 C2 pic X(20) value 'Copyright 1987-2012 '.
05 C3 pic X(28) value '--- SimoTime Enterprises ---'.
05 C4 pic X(20) value ' All Rights Reserved'.
01 FIRST-TIME pic X value 'Y'.
01 A-COMPILE-TIME pic X value 'A'.
01 A-EBC pic X value x'C1'.
01 A-ASC pic X value x'41'.
01 COMPRESS pic X(8) value 'COMPRESS'.
01 EXPAND pic X(8) value 'EXPAND '.
01 BIT-ON pic 9 value 1.
01 BIT-OFF pic 9 value 0.
01 MESSAGE-BUFFER.
05 MESSAGE-HEADER pic X(11) value '* SIMOBITS '.
05 MESSAGE-TEXT pic X(68).
01 TWO-BYTES.
05 TWO-BYTES-01 pic X.
05 TWO-BYTES-02 pic X.
01 TWO-BYTES-BINARY redefines TWO-BYTES
pic S9(4) binary.
01 IX-1 pic 99 value 0.
01 REGISTER-1 pic S9(5) value 0.
*****************************************************************
LINKAGE SECTION.
COPY PASSBITS.
*****************************************************************
PROCEDURE DIVISION using BTS-PASS-AREA.
MAIN-ROUTINE.
move ZERO to BTS-PASS-RESULT
if FIRST-TIME not = 'N'
perform Z-POST-COPYRIGHT
move 'N' to FIRST-TIME
end-if
evaluate BTS-PASS-REQUEST
when COMPRESS perform COMPRESS-BYTES
when EXPAND perform EXPAND-BITS
when OTHER perform INVALID-REQUEST
end-evaluate
add BTS-PASS-RESULT to ZERO giving RETURN-CODE
GOBACK.
*****************************************************************
INVALID-REQUEST.
display '* SIMOBITS, INVALID REQUEST, ' BTS-PASS-REQUEST
add 16 to ZERO giving BTS-PASS-RESULT
exit.
*****************************************************************
* Input: BTS-PASS-BYTES, an eight byte field
* Output: BTS-PASS-BITS, a one byte field (8-bits)
*
* For each byte that is a one in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to ON (1)
*
* For each byte that is zero in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to OFF (0).
*
* Example:
* BTS-PASS-BYTES = '01010101' then BTS-PASS-BITS set to x'55'
*****************************************************************
COMPRESS-BYTES.
subtract TWO-BYTES-BINARY from TWO-BYTES-BINARY
add 1 to ZERO giving IX-1
add 128 to ZERO giving REGISTER-1
perform until IX-1 GREATER THAN 8
if BTS-PASS-BYTES(IX-1:1) = BIT-ON
add REGISTER-1 to TWO-BYTES-BINARY
else
if BTS-PASS-BYTES(IX-1:1) not = BIT-OFF
move '8-Byte string must be zeroes or ones...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
add 8 to IX-1
add 8 to ZERO giving BTS-PASS-RESULT
end-if
end-if
divide 2 INTO REGISTER-1
add 1 to IX-1
end-perform
move TWO-BYTES-02 to BTS-PASS-BITS
exit.
*****************************************************************
* Input: BTS-PASS-BITS, a one byte field (8-bits)
* OUTPUT: BTS-PASS-BYTES, an eight byte field
*
* For each bit that is on in BTS-PASS-BITS set the
* corresponding byte in BTS-PASS-BYTES to a value of one.
*
* For each bit that is off in BTS-PASS-BITS set the
* corresponding byte in BTS-PASS-BYTES to a value of zero.
*
* Example:
* BTS-PASS-BITS = x'55' then BTS-PASS-BYTES will be '01010101'
*****************************************************************
EXPAND-BITS.
subtract TWO-BYTES-BINARY from TWO-BYTES-BINARY
add 1 to ZERO giving IX-1
add 128 to ZERO giving REGISTER-1
move BTS-PASS-BITS to TWO-BYTES-02
perform 8 times
if TWO-BYTES-BINARY = REGISTER-1
or TWO-BYTES-BINARY > REGISTER-1
move BIT-ON to BTS-PASS-BYTES(IX-1:1)
subtract REGISTER-1 from TWO-BYTES-BINARY
else
move BIT-OFF to BTS-PASS-BYTES(IX-1:1)
end-if
divide 2 INTO REGISTER-1
add 1 to IX-1
end-perform
exit.
*****************************************************************
* Display the Copyright data-structure...
* ------------------------------------------------------------
Z-POST-COPYRIGHT.
display SIM-TITLE upon console
display SIM-COPYRIGHT upon console
exit.
Z-POST-MESSAGE.
display MESSAGE-BUFFER upon console
move SPACES to MESSAGE-TEXT
exit.
*****************************************************************
* This example is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
This copy file (PASSBITS.CPY) provides a convenient method for defining the data areas (or fields) that are used to pass information between the demo program and the bit and byte conversion routine.
*****************************************************************
* PASSBITS is a COBOL Copy File *
* Data Structure or Pass Area used for calling SIMOBITS. *
* Copyright (C) 1987-2012 SimoTime Enterprises *
* All Rights Reserved *
*****************************************************************
* Provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
*
* When BTS-PASS-REQUEST is 'EXPAND ' then
* BTS-PASS-BITS (a 1-btye field of 8-bits) is analyzed
* and based on the bit settings a corresponding value of
* 0 or 1 is set in the BTS-PASS-BYTES (an 8-byte field
* of zeroes and ones).
*
* When BTS-PASS-REQUEST is 'COMPRESS' then
* BTS-PASS-BYTES (an 8-byte field of zeroes and ones) is
* analyzed and based on a value of 0 or 1 the corresponding
* bit in BTS-PASS-BITS (a 1-byte field of 8-bits) is
* switched ON or OFF.
*
01 BTS-PASS-AREA.
05 BTS-PASS-REQUEST PIC X(8).
05 BTS-PASS-RESULT PIC S9(9) COMP.
05 BTS-PASS-RECORD.
10 BTS-PASS-BITS PIC X.
10 BTS-PASS-BYTES.
15 BTS-PASS-BYTE-01 PIC X.
15 BTS-PASS-BYTE-02 PIC X.
15 BTS-PASS-BYTE-03 PIC X.
15 BTS-PASS-BYTE-04 PIC X.
15 BTS-PASS-BYTE-05 PIC X.
15 BTS-PASS-BYTE-06 PIC X.
15 BTS-PASS-BYTE-07 PIC X.
15 BTS-PASS-BYTE-08 PIC X.
*
*** PASSBITS - End-of-Copy File - - - - - - - - - - - PASSBITS *
*****************************************************************
*
The purpose of this document is to assist as a tutorial for new programmers or as a quick reference for experienced programmers. In the world of programming there are many ways to solve a problem. This document and the links to other documents are intended to provide a choice of alternatives.
This suite of programs is provided as a COBOL programming example of one of the possible solutions to the problem of determining and changing the bits within a byte. This example uses an approach of converting the bit information in a single byte to or from an eight-byte field of COBOL accessible zeroes and ones.
Permission to use, copy, modify and distribute this software, documentation or training material for any purpose requires a fee to be paid to SimoTime Enterprises. Once the fee is received by SimoTime the latest version of the software, documentation or training material will be delivered and a license will be granted for use within an enterprise, provided the SimoTime copyright notice appear on all copies of the software. The SimoTime name or Logo may not be used in any advertising or publicity pertaining to the use of the software without the written permission of SimoTime Enterprises.
SimoTime Enterprises makes no warranty or representations about the suitability of the software, documentation or learning material for any purpose. It is provided "AS IS" without any expressed or implied warranty, including the implied warranties of merchantability, fitness for a particular purpose and non-infringement. SimoTime Enterprises shall not be liable for any direct, indirect, special or consequential damages resulting from the loss of use, data or projects, whether in an action of contract or tort, arising out of or in connection with the use or performance of this software, documentation or training material.
This section includes links to documents with additional information that is beyond the scope and purpose of this document. The first sub-section requires an internet connection, the second sub-section references locally available documents.
Note: A SimoTime License is required for the items to be made available on a local server.
The following links will require an internet connect.
A good place to start is The SimoTime Home Page for access to white papers, program examples and product information.
You may download this example at the download list of evaluation packages.
The callable COBOL program that does bit manipulation was originally an Assembler routine. An example that uses a callable Assembler routine is available. Refer to Assembler does Bit Manipulation example for more information.
Explore The ASCII and EBCDIC translation tables. These tables are provided for individuals that need to better understand the bit structures and differences of the encoding formats.
Explore The File Status Return Codes to interpret the results of accessing VSAM data sets and QSAM files.
Explore The Micro Focus Web Site for more information about products and services available from Micro Focus.
The following links may be accessible without an internet connection.
Explore The File Status Return Codes to interpret the results of accessing VSAM data sets and QSAM files.
Check out The SimoTime Glossary for a list of terms and definitions used in the documents provided by SimoTime.
This document was created and is maintained by SimoTime Enterprises.
If you have any questions, suggestions, comments or feedback please call or send an e-mail to: helpdesk@simotime.com
We appreciate hearing from you.
Founded in 1987, SimoTime Enterprises is a privately owned company. We specialize in the creation and deployment of business applications using new or existing technologies and services. We have a team of individuals that understand the broad range of technologies being used in today's environments. This includes the smallest thin client using the Internet and the very large mainframe systems. There is more to making the Internet work for your company's business than just having a nice looking WEB site. It is about combining the latest technologies and existing technologies with practical business experience. It's about the business of doing business and looking good in the process. Quite often, to reach larger markets or provide a higher level of service to existing customers it requires the newer Internet technologies to work in a complementary manner with existing corporate mainframe systems.
Whether you want to use the Internet to expand into new market segments or as a delivery vehicle for existing business functions simply give us a call or check the web site at http://www.simotime.com
| Return-to-Top |
| COBOL Bit Processing, Bit Manipulation |
| Copyright © 1987-2012 SimoTime Enterprises All Rights Reserved |
| When technology complements business |
| http://www.simotime.com |