COBOL Bit Processing
When technology complements business   Bit Manipulation
Copyright © 1987-2008  SimoTime Enterprises, LLC  All Rights Reserved http://www.simotime.com

 
Introduction Version 06.11.08
 
  Expand, Bits to Bytes
  Compress, Bytes to Bits
  Call Interface
  CMD Member for Execution with Net Express
  JCL Member for OS/390 or Mainframe Express
  The COBOL Demonstration Program
  The COBOL Bit-Manipulation Routine
  The Copy File for Linkage Data Areas
  Summary
 
  Software Agreement and Disclaimer
  Downloads and Links to Similar Pages
  Comments or Suggestions
  About SimoTime

Introduction
(Next) (Previous) (Table-of-Contents)

This is an example of how COBOL can do bit-level manipulation. The three basic functions provided are as follows.

  1. Determine the setting of a bit (0 or 1).
  2. Set a bit to 0.
  3. Set a bit to 1.

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 (refer to http://www.microfocus.com ). 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 is provided in the Downloads and Links to Similar Pages section of this document.

Note: The source code for this example is available from the SimoTime Library under Download Directory at   www.simotime.com

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 ongong support of an assembler routine in a COBOL programming shop may not be worth these advanatges. 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.

EXPAND - translate the bits of a one-byte field to bytes of an eight-byte field
(Next) (Previous) (Table-of-Contents)

For each bit that is ON (1) in the BTS-PASS-BITS field the correspondong 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 correspondong byte in the BTS-PASS-BYTES field is set to a value of zero.

  Input BTS-PASS-BITS, a one byte field (8-bits)
  Output BTS-PASS-BYTES, an eight byte field
  Example  if  BTS-PASS-BITS = x'55'  then  BTS-PASS-BYTES will be '01010101'
     

COMPRESS - translate the bytes of an eight-byte field into bits of a one-byte field
(Next) (Previous) (Table-of-Contents)

For each byte that is a one in the BTS-PASS-BYTES field the correspondong 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 correspondong bit in the BTS-PASS-BITS field is set to OFF (0).

  Input BTS-PASS-BYTES, an eight byte field
  Output BTS-PASS-BITS, a one byte field (8-bits)
  Example  if   BTS-PASS-BYTES = '01010101'  then  BTS-PASS-BITS will be set to x'55'
     

Call Interface
(Next) (Previous) (Table-of-Contents)

The callable interface requires a data structure to be defined. A copy file (PASSBITS.CPY) is provided with the following source code.

      *****************************************************************
      *           Data Structure used for calling SIMOBITS.           *
      *****************************************************************
      *         Copyright (C) 1987-2006 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       *
      *****************************************************************
       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.

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 CMD Member
(Next) (Previous) (Table-of-Contents)

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  *                   This program is provided by:                    *
rem  *                    SimoTime Enterprises, LLC                      *
rem  *           (C) Copyright 1987-2006 All Rights Reserved             *
rem  *                                                                   *
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 - 04.01.09
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  *
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  *   * SIMOEXEC *
rem  *   ********exe*
rem  *        *
rem  *        *
rem  *   ************     ************
rem  *   * CBLBITC1 ******* CONSOLE  *
rem  *   ********cbl*     ************
rem  *        *   *
rem  *        *   *       ************
rem  *        *   ******** SIMOBITS  *
rem  *        *           ********cbl*
rem  *        *
rem  *        *
rem  *        *
rem  *   ************
rem  *   *   EOJ    *
rem  *   ************
rem  *
rem  * ********************************************************************
rem  * Step   1 of 2  Set the global environment variables...
rem  *
     set syslog=d:\simoNXE4\AN01\datawrk1\SYSLOGT1.TXT
rem  *
     SimoEXEC NOTE  *******************************************************CblBitE1
     SimoEXEC NOTE  Starting JobName CblBitE1
rem  * ********************************************************************
rem  * Step   2 of 2  Execute the sample program...
rem  *
     SimoEXEC EXEC CblBitC1
rem  *
     SimoEXEC NOTE Finished JobName CblBitE1
pause

The JCL Member
(Next) (Previous) (Table-of-Contents)

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,ACCOUNT,CLASS=1,MSGCLASS=0,NOTIFY=CSIP1
//* *******************************************************************
//*                   This program is provided by:                    *
//*                    SimoTime Enterprises, LLC                      *
//*           (C) Copyright 1987-2006 All Rights Reserved             *
//*                                                                   *
//*             Web Site URL:   http://www.simotime.com               *
//*                   e-mail:   helpdesk@simotime.com                 *
//* *******************************************************************
//*
//* Text   - COBOL calls COBOL for Bit and Byte maniplulation
//* 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.
//*
//* 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   *
//*

The COBOL Demonstration Program
(Next) (Previous) (Table-of-Contents)

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-2006 SimoTime Enterprises, LLC.            *
      *                                                               *
      * 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.This software contains confidential information   *
      *                                                               *
      * 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 express 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 ' v04.01.09'.
           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-2006 '.
           05  C3 pic X(28) value '  SimoTime Enterprises, LLC '.
           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 sample '.
           05  C3 pic X(32) value 'by SimoTime Enterprises, LLC    '.
           05  C4 pic X(04) value '    '.

       01  SIM-THANKS-02.
           05  C1 pic X(11) value '* CBLBITC1 '.
           05  C2 pic X(32) value 'Please send comments or suggesti'.
           05  C3 pic X(32) value 'ons to helpdesk@simotime.com    '.
           05  C4 pic X(04) value '    '.

       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 615.
       01  FIVE-BYTES              pic X(5)   value LOW-VALUES.
       01  TWO-BYTES               pic X(2)   value '00'.
       01  EIGHT-BYTES             pic X(8)   value LOW-VALUES.
       01  UNPACKED-5              pic 9(5)   value 0.

       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.

           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

      *!   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 reults...
           display '* CBLBITC1 '
                   'The Easy way... '
                   'PACK-03=' PACK-03
                   '  UNPACKED-5=' UNPACKED-5
           upon console

           display '* CBLBITC1 '
                   'The Hard way... '
                   'PACK-03=' PACK-03
                   '  FIVE-BYTES=' FIVE-BYTES
           upon console

           move 'Finished the COBOL-UNPACK Routine...'
             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     upon console
           display SIM-COPYRIGHT upon console
           exit.

      *****************************************************************
       Z-POST-MESSAGE.
           display MESSAGE-BUFFER upon console
           move SPACES to MESSAGE-TEXT
           exit.

      *****************************************************************
       Z-THANK-YOU.
           display SIM-THANKS-01 upon console
           display SIM-THANKS-02 upon console
           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       *
      *****************************************************************

The COBOL Bit-Manipulation Routine
(Next) (Previous) (Table-of-Contents)

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-2006 SimoTime Enterprises, LLC.            *
      *                                                               *
      * 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 express 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 ' v03.01.24'.
           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-2006 '.
           05  C3 pic X(28) value '  SimoTime Enterprises, LLC '.
           05  C4 pic X(20) value ' All Rights Reserved'.

       01  FIRST-TIME              pic X       value 'Y'.

       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.
           subtract BTS-PASS-RESULT from 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) = '1'
                   add REGISTER-1 to TWO-BYTES-BINARY
               else
                   if  BTS-PASS-BYTES(IX-1:1) not = '0'
                       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 '1' to BTS-PASS-BYTES(IX-1:1)
                   subtract REGISTER-1 from TWO-BYTES-BINARY
               else
                   move '0' 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 program provides very little visual information when it is executed on the mainframe. The real value of this program is observed when it is animated using Mainframe Express provided by Micro Focus. It is possible to watch the actual execution of each individual instruction and to immediately see the results.

The Copy File for Linkage Data Areas
(Next) (Previous) (Table-of-Contents)

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.

      *****************************************************************
      *           Data Structure used for calling SIMOBITS.           *
      *****************************************************************
      *         Copyright (C) 1987-2003 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       *
      *****************************************************************
       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.

Summary
(Next) (Previous) (Table-of-Contents)

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

Software Agreement and Disclaimer
(Next) (Previous) (Table-of-Contents)

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

If you have any questions, suggestions or comments please call or send an e-mail to: helpdesk@simotime.com

Downloads and Links to Similar Pages
(Next) (Previous) (Table-of-Contents)

You may download this example at  http://www.simotime.com/sim4dzip.htm#COBOLBitManipulation  as a Z-Pack. The Z-Packs provide individual programming examples, documentation and test data files in a single package. The Z-Packs are usually in zip format to reduce the amount of time to download.

Please view the complete list of SimoTime Z-Pack Examples at  http://www.simotime.com/sim4dzip.htm .

Note: You must be attached to the Internet to download a Z-Pack or view the list.

The  hexadecimal dump  of the parameter-buffer uses the same technique as describe in another SimoTime example that describes the dumping of a data string using COBOL. The name of the member that does the actual hexadecimal dump is called SimoDUMP. A copy file (PASSDUMP.CPY) is provided for defining the pass area.

The  SimoZAPS Utility Program  has the capability of generating a COBOL program that will do the conversion of sequential and VSAM (KSDS) files between EBCDIC and ASCII. SimoZAPS can also read a sequential file in EBCDIC format and create an ASCII/CRLF file or VSAM KSDS file in ASCII format. The conversion tables may be viewed or modified to meet unique requirements. The Hexcess/2 function provides the capability of viewing, finding or patching the contents of a file in hexadecimal.

Check out  The COBOL Connection  for more examples of mainframe COBOL coding techniques and sample code.

Check out  The SimoTime Library  for a wide range of topics for Programmers, Project Managers and Software Developers.

To review all the information available on this site start at  The SimoTime Home Page .

Comments or Suggestions
(Next) (Previous) (Table-of-Contents)

If you have any questions, suggestions or comments please call or send an e-mail to: helpdesk@simotime.com.

About SimoTime Enterprises, LLC
(Next) (Previous) (Table-of-Contents)

Founded in 1987, SimoTime Enterprises is a privately owned, Limited Liability Corporation located in Novato, California. 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
Copyright © 1987-2008 SimoTime Enterprises, LLC  All Rights Reserved
When technology complements business
http://www.simotime.com
Version 06.11.08