Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Cobol bit level operation

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
rakesh_mishra18

New User


Joined: 15 May 2006
Posts: 23
Location: Jamshedpur

PostPosted: Mon Oct 20, 2008 2:11 am    Post subject: Cobol bit level operation
Reply with quote

Is Bit level operation possible in Cobol. Pls provide some example.
Back to top
View user's profile Send private message

Anuj Dhawan

Senior Member


Joined: 22 Apr 2006
Posts: 6258
Location: Mumbai, India

PostPosted: Mon Oct 20, 2008 2:30 am    Post subject:
Reply with quote

Hello Rakesh,

You seem to have two differnt question, when you have differnt questions you should have differnt threads, per forum rules. Your other question in this thread is been spiltted to

Need to know limitaion of REXX over JCL.

-Ad
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8055
Location: East Dubuque, Illinois, USA

PostPosted: Mon Oct 20, 2008 2:45 am    Post subject:
Reply with quote

Have you read the manual?
Back to top
View user's profile Send private message
Anuj Dhawan

Senior Member


Joined: 22 Apr 2006
Posts: 6258
Location: Mumbai, India

PostPosted: Mon Oct 20, 2008 2:53 am    Post subject:
Reply with quote

Hi,

Here is a similar link..

http://ibmmainframes.com/viewtopic.php?t=32867&highlight=bit
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Mon Oct 20, 2008 4:56 am    Post subject:
Reply with quote

Hi Rakesh,

I wrote the following as an exercise some time back. It should give you an idea of how to manipulate the individual bits in a byte.
Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID.    BITFLIPR.
       AUTHOR.        JACK.
       ENVIRONMENT    DIVISION.
      ******************************************************************
      *  CHANGES THE REQUESTED BIT OF THE BYTE PASSED BY THE CALLER.   *
      *  THIS SUB PGM CAN BE CALLED FROM A PGM OR EXECUTED DIRECTLY    *
      *  FROM JCL USING THE PARM FIELD IN THE EXEC CARD. THE JCL EX-   *
      *  ECUTION IS INTENDED TO FACILITATE TESTING.                    *
      *                                                                *
      *  WHEN INVOKED FROM A PGM, USE 1 OF THE FOLLOWING CALLING SEQ:  *
      *                                                                *
      *  CALL 'BITFLIPR' USING LK-PARM-LEN LK-PARM-DTA                 *
      *  CALL WS-PGMNAME USING LK-PARM-LEN LK-PARM-DTA                 *
      *                                                                *
      * WHERE,                                                         *
      *                                                                *
      *  WS-PGMNAME CONTAINS THE 8 POSITION LITERAL 'BITFLIPR'.        *
      *  LK-PARM-LEN IS AN 01 LEVEL DEFINED AS IN THE TABLE BELOW.     *
      *  LK-PARM-DTA IS AN 01 LEVEL DEFINED AS FOLLOWS:                *
      *                                                                *
      *     01  LK-PARM-DATA.                                          *
      *         05  LK-TGT-BYTE                                        *
      *         05  LK-BIT-NBR                                         *
      *         05  LK-ACTION                                          *
      *                                                                *
      * WHERE THE 05 LEVELS ARE DEFINED AS IN THE TABLE BELOW.         *
      *                                                                *
      *         DESCRIPTION       PICTURE              COMMENTS        *
      *         *************   **********    **************************
      *         PARM LENGTH     S9(4) COMP    CALLED FROM PGM THIS FLD *
      *                                       MUST HAVE VALUE OF -1.   *
      *                                       CALLED FROM JCL THIS FLD *
      *                                       MUST BE OMMITTED.        *
      *         TARGET BYTE      X(1)         THE BYTE THE CALLER      *
      *                                       WANTS CHANGED.           *
      *         BIT NUMBER       9(1)         THE BIT THE CALLER       *
      *                                       WANTS CHANGED. BIT NUM-  *
      *                                       BERS RANGE FROM 1 TO 8   *
      *                                       FROM HI TO LOW ORDER.    *
      *         ACTION           X(1)         THIS FLD HAS 1 OF 3 VAL- *
      *                                       UES: "1" - TURN BIT ON   *
      *                                            "0" - TURN BIT OFF  *
      *                                            "F" - FLIP THE BIT  *
      *                                                                *
      * REMEMBER THAT THE TARGET BYTE IS UPDATED BY 'BITFLIPR'.        *
      *                                                                *
      ******************************************************************

       CONFIGURATION    SECTION.
       DATA DIVISION.
       FILE SECTION.

      ******************************************************************
       WORKING-STORAGE  SECTION.
      ******************************************************************

      ******************************************************************
       01  WS-TABLES.
      ******************************************************************

      ******************************************************************
      *                                                                *
      * THIS TABLE IS USED TO DETERMINE THE VALUE THAT THE SUBJECT     *
      * BYTE MUST BE MULTIPLIED BY (MULT-VALUE) TO DETERMINE IF IT IS  *
      * CURRENTLY ON OR OFF (1 OR 0).                                  *
      *                                                                *
      * THE TABLE IS ALSO USED TO DETERMINE THE INCREMENT/DECREMENT    *
      * REQUIRED TO TURN THE BIT ON OR OFF (ADD-SUB-VALUE).            *
      *                                                                *
      ******************************************************************
           05  MULT-ADD-SUB-VALUES.
               10  BIT1-MULT-VALUE     PIC S9(004) VALUE +0    COMP.
               10  BIT1-ADD-SUB-VALUE  PIC S9(004) VALUE +128  COMP.
               10  BIT2-MULT-VALUE     PIC S9(004) VALUE +2    COMP.
               10  BIT2-ADD-SUB-VALUE  PIC S9(004) VALUE +64   COMP.
               10  BIT3-MULT-VALUE     PIC S9(004) VALUE +4    COMP.
               10  BIT3-ADD-SUB-VALUE  PIC S9(004) VALUE +32   COMP.
               10  BIT4-MULT-VALUE     PIC S9(004) VALUE +8    COMP.
               10  BIT4-ADD-SUB-VALUE  PIC S9(004) VALUE +16   COMP.
               10  BIT5-MULT-VALUE     PIC S9(004) VALUE +16   COMP.
               10  BIT5-ADD-SUB-VALUE  PIC S9(004) VALUE +8    COMP.
               10  BIT6-MULT-VALUE     PIC S9(004) VALUE +32   COMP.
               10  BIT6-ADD-SUB-VALUE  PIC S9(004) VALUE +4    COMP.
               10  BIT7-MULT-VALUE     PIC S9(004) VALUE +64   COMP.
               10  BIT7-ADD-SUB-VALUE  PIC S9(004) VALUE +2    COMP.
               10  BIT8-MULT-VALUE     PIC S9(004) VALUE +128  COMP.
               10  BIT8-ADD-SUB-VALUE  PIC S9(004) VALUE +1    COMP.

           05  REDEFINES MULT-ADD-SUB-VALUES.
           10  BIT-ARITH-ENTRY         OCCURS 8 TIMES.
               20  MULT-VALUE          PIC S9(004).
               20  ADD-SUB-VALUE       PIC S9(004).

      ******************************************************************
       01  WS-WORK-FIELDS.
      ******************************************************************

           05  COMPARE-WRK-FLD         PIC S9(004) VALUE 0    COMP.
           05  REDEFINES COMPARE-WRK-FLD.
               10  FILLER              PIC  X(001).
               10  COMPARE-WRK-BYTE    PIC  X(001).
                   88  BIT-IS-ON       VALUE X'80' THRU X'FF'.
                   88  BIT-IS-OFF      VALUE X'00' THRU X'7F'.

           05  ADD-SUB-WRK-FLD         PIC S9(004) VALUE 0    COMP.
           05  REDEFINES ADD-SUB-WRK-FLD.
               10  FILLER              PIC  X(001).
               10  ADD-SUB-WRK-BYTE    PIC  X(001).
1     /
      ******************************************************************
       LINKAGE                         SECTION.
      ******************************************************************

       01  LK-PARM-LEN                 PIC S9(004).
           88  LK-EXECUTED-FROM-PGM        VALUE -1.
           88  LK-EXECUTED-FROM-JCL        VALUE +0 THRU +9999.

       01  LK-PARM-DATA.
           05  LK-TGT-BYTE                 PIC  X(001).
           05  LK-BIT-NBR                  PIC  9(001).
               88  VALID-BIT-NBR               VALUE +1 THRU +8.
           05  LK-ACTION                   PIC  X(001).
               88  LK-TURN-BIT-ON              VALUE '1'.
               88  LK-TURN-BIT-OFF             VALUE '0'.
               88  LK-FLIP-THE-BIT             VALUE 'F'.
               88  VALID-ACTION                VALUE '1' '0' 'F'.
1     /
      ******************************************************************
       PROCEDURE DIVISION USING LK-PARM-LEN LK-PARM-DATA
      ******************************************************************
       000-MAINLINE.
      ******************************************************************

           PERFORM 8000-VALIDATE-PARM-INFO
           IF RETURN-CODE = ZEROS
              PERFORM 1000-MODIFY-BIT-VALUE
           ELSE
              IF LK-EXECUTED-FROM-JCL
                 DISPLAY 'ERROR IN THE PARM LIST. RETURN CODE SHOWS '
                         'THE PARM# IN ERROR'
                 DISPLAY ' '
                 DISPLAY 'RETURN CODE >' RETURN-CODE '<'
              END-IF
           END-IF
           GOBACK
           .
      ******************************************************************
       1000-MODIFY-BIT-VALUE.
      ******************************************************************
      *===>  SAVE TARGET BYTE
      *---------------------------------------------------------------

           MOVE LK-TGT-BYTE            TO  COMPARE-WRK-BYTE
           MOVE LK-TGT-BYTE            TO  ADD-SUB-WRK-BYTE

      *---------------------------------------------------------------
      *===>  ISOLATE BIT TO BE CHANGED. MULTIPLY THE TGT BYTE BY THE
      *===>  BINARY VALUE IN THE TABLE (POINTED TO BY THE BIT NUMBER
      *===>  PASSED BY THE CALLER). THIS MOVES THE TGT BIT TO THE HI
      *===>  ORDER POS OF THE WRK BYTE.
      *---------------------------------------------------------------

           COMPUTE COMPARE-WRK-FLD      = COMPARE-WRK-FLD
                                        * MULT-VALUE(LK-BIT-NBR)

      *---------------------------------------------------------------
      *===>  CHANGE THE BIT AS DIRECTED BY THE ACTION FLD PROVIDED BY
      *===>  CALLER.IF THE BYTE VALUE OF COMPARE-WRK-BYTE IS > X'7F',
      *===>  THE REQUESTED BIT IS ONE;IF <= THE BIT IS ZERO.
      *===>  ADD TABLE VALUE IF BIT IS ZERO(OFF); SUB IF IT'S ONE(ON).
      *===>  THIS PREVENTS CHANGES TO SURROUNDING BITS WHILE CHANGING
      *===>  THE REQUESTED BIT.
      *---------------------------------------------------------------

           EVALUATE TRUE
           WHEN LK-TURN-BIT-ON
                IF BIT-IS-OFF
                COMPUTE ADD-SUB-WRK-FLD = ADD-SUB-WRK-FLD
                                        + ADD-SUB-VALUE(LK-BIT-NBR)
                END-IF
           WHEN LK-TURN-BIT-OFF
                IF BIT-IS-ON
                COMPUTE ADD-SUB-WRK-FLD = ADD-SUB-WRK-FLD
                                        - ADD-SUB-VALUE(LK-BIT-NBR)
                END-IF
           WHEN LK-FLIP-THE-BIT
                IF BIT-IS-ON
                COMPUTE ADD-SUB-WRK-FLD = ADD-SUB-WRK-FLD
                                        - ADD-SUB-VALUE(LK-BIT-NBR)
                ELSE
                COMPUTE ADD-SUB-WRK-FLD = ADD-SUB-WRK-FLD
                                        + ADD-SUB-VALUE(LK-BIT-NBR)
                END-IF
           END-EVALUATE

      *---------------------------------------------------------------
      *===>  IF SUBPGM WAS EXECUTED FROM JCL, I.E. A TEST, DISPLAY
      *===>  THE BEFORE AND AFTER VALUES OF THE TARGET FIELD. ELSE
      *===>  MOVE IT TO THE LINKAGE FIELD FOR RETURN TO THE CALLER.
      *---------------------------------------------------------------

           IF LK-EXECUTED-FROM-JCL
              DISPLAY 'THE TARGET BYTE WAS CHANGED FROM >'
                      LK-TGT-BYTE '< TO >' ADD-SUB-WRK-BYTE '<'
           ELSE
              MOVE ADD-SUB-WRK-BYTE  TO LK-TGT-BYTE
           END-IF
           .
      ******************************************************************
       8000-VALIDATE-PARM-INFO.
      ******************************************************************

           EVALUATE TRUE
           WHEN LK-BIT-NBR NOT NUMERIC
           WHEN NOT VALID-BIT-NBR
                MOVE 3 TO RETURN-CODE
           WHEN NOT VALID-ACTION
                MOVE 4 TO RETURN-CODE
           END-EVALUATE
           .
      *===>  END OF PGM CODE
Back to top
View user's profile Send private message
Kevin Wailes

New User


Joined: 29 Aug 2008
Posts: 10
Location: UK

PostPosted: Mon Oct 20, 2008 12:26 pm    Post subject: Reply to: Cobol bit level operation
Reply with quote

There are LE callable services which allow for bit manipulation. Look in the LE programming guide.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2502
Location: Atlanta, Georgia, USA

PostPosted: Tue Oct 21, 2008 9:27 pm    Post subject: Re: Cobol bit level operation
Reply with quote

It's easy enough to do with binary arithmetic. Move your value to a binary-fullword, which is also redefined as a PIC X(04). Then, determine which bit you'd like to check.

For example, if you need to check the 2nd-bit from the left, multiply the fullword by 2 and then check the 4th-byte of the fullword for a value greater than X'7F' (a poor man's "TM").

If it is on and you want to turn it off, then subtract 64 from the fullword (a poor man's "AND").

If it is off and you want to turn it on, then add 64 to the fullword (a poor man's "OR").

Because of the affect the TRUNC option can have on non COMP-5 halfword's, always use a fullword.

Although the TRUNC option does not have any affect on COMP-5 (Native Binary), always use a fullword.

HTH....

Regards,

Bill
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2502
Location: Atlanta, Georgia, USA

PostPosted: Tue Oct 21, 2008 9:31 pm    Post subject: Re: Cobol bit level operation
Reply with quote

One tidbit which I need to point out is the fullword is a workarea and turning bits on and off via addition and subtraction needs to be performed against the original value, which had been moved to a more "permanent" fullword-area, different than the fullword-workarea.

Regards,

Bill
Back to top
View user's profile Send private message
zos5415

New User


Joined: 29 Aug 2008
Posts: 18
Location: China

PostPosted: Wed Dec 02, 2009 2:51 pm    Post subject:
Reply with quote

Code:

           05  MULT-ADD-SUB-VALUES.
               10  BIT1-MULT-VALUE     PIC S9(004) VALUE +0    COMP.   ==> it should be 1 , right ?
               10  BIT1-ADD-SUB-VALUE  PIC S9(004) VALUE +128  COMP.
               10  BIT2-MULT-VALUE     PIC S9(004) VALUE +2    COMP.
               10  BIT2-ADD-SUB-VALUE  PIC S9(004) VALUE +64   COMP.
               10  BIT3-MULT-VALUE     PIC S9(004) VALUE +4    COMP.
               10  BIT3-ADD-SUB-VALUE  PIC S9(004) VALUE +32   COMP.
               10  BIT4-MULT-VALUE     PIC S9(004) VALUE +8    COMP.
               10  BIT4-ADD-SUB-VALUE  PIC S9(004) VALUE +16   COMP.
               10  BIT5-MULT-VALUE     PIC S9(004) VALUE +16   COMP.
               10  BIT5-ADD-SUB-VALUE  PIC S9(004) VALUE +8    COMP.
               10  BIT6-MULT-VALUE     PIC S9(004) VALUE +32   COMP.
               10  BIT6-ADD-SUB-VALUE  PIC S9(004) VALUE +4    COMP.
               10  BIT7-MULT-VALUE     PIC S9(004) VALUE +64   COMP.
               10  BIT7-ADD-SUB-VALUE  PIC S9(004) VALUE +2    COMP.
               10  BIT8-MULT-VALUE     PIC S9(004) VALUE +128  COMP.
               10  BIT8-ADD-SUB-VALUE  PIC S9(004) VALUE +1    COMP.

           05  REDEFINES MULT-ADD-SUB-VALUES.
           10  BIT-ARITH-ENTRY         OCCURS 8 TIMES.
               20  MULT-VALUE          PIC S9(004).   ==> it should be comp type?
               20  ADD-SUB-VALUE       PIC S9(004). ==> it should be comp type?

Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
This topic is locked: you cannot edit posts or make replies. Cobol GUI (like Editor) Kala Mainframe COBOL Programming 14 Wed May 10, 2017 12:30 pm
No new posts IMS T-Pipe queue counts in a COBOL Pr... Siva NKK Kothamasu IMS DB/DC 0 Tue May 09, 2017 6:31 pm
No new posts COBOL Code Parsers and Lineage Establ... balimanja COBOL Programming 2 Tue May 02, 2017 3:30 am
No new posts Cobol upgrade - source code missing f... gthmrj IBM Tools 1 Wed Apr 26, 2017 6:04 pm
No new posts COBOL Programming Sandpit? jodrisco COBOL Programming 6 Wed Apr 12, 2017 3:47 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us