IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How can I read binary bits in cobol?


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sourabh jain

New User


Joined: 29 Feb 2008
Posts: 27
Location: Pune

PostPosted: Tue Jul 29, 2008 2:09 pm
Reply with quote

I am trying to populate all the 16 bits of a 2 byte comp field. The intention is to read these bits seperately and perform some functions depending upon which all bits are set to 1. Would you please help me in finding out any way to do so? Anything that could be helpful here is welcome.
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Tue Jul 29, 2008 2:37 pm
Reply with quote

PL1 will be ideal for such a situation.
Back to top
View user's profile Send private message
sourabh jain

New User


Joined: 29 Feb 2008
Posts: 27
Location: Pune

PostPosted: Tue Jul 29, 2008 2:40 pm
Reply with quote

Hi Srihari, Thanks for your response.
I am using IDMS as backend. I am not aware of what PL1 is? Pls let me know if I can use it, and how?
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Jul 29, 2008 2:44 pm
Reply with quote

PL1 is another programming language where you can define fields as bits ...

chk this link for bit manipulation in cobol

www.simotime.com/cblbit01.htm
Back to top
View user's profile Send private message
UmeySan

Active Member


Joined: 22 Aug 2006
Posts: 771
Location: Germany

PostPosted: Tue Jul 29, 2008 4:53 pm
Reply with quote

Hi !

You could use LE services in Cobol to test and to set certain bit's of a Field.
Example: CALL 'CEESITST' USING Byte-2-check Bit-number FC Result
Just google the web with CEESITST, and you will get a lot of examples.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


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

PostPosted: Wed Jul 30, 2008 12:32 am
Reply with quote

The following program is passed a 16-byte parm, consisting of '1's and '0's, which is then converted into its binary-halfword complement.

For example, if you pass a parm of '1010101010101010', the program informs you of its decimal-value 43690 and its hex-value X'AAAA'. The resulting binary-value can be found in field WS-FWORD.

Code:

       IDENTIFICATION DIVISION.                                           
       PROGRAM-ID. CVTBITS.                                               
       ENVIRONMENT DIVISION.                                             
       DATA DIVISION.                                                     
       WORKING-STORAGE SECTION.                                           
       01  WS-DATA.                                                       
           03  WS-DISPLAY          PIC  9(017).                           
           03  WS-DISPLAY-X        REDEFINES WS-DISPLAY                   
                                   PIC  X(017).                           
           03  WS-DISPLAY-V9       REDEFINES WS-DISPLAY                   
                                   PIC  9(016)V9.                         
           03  WS-PACKED           PIC  9(017)     PACKED-DECIMAL.       
           03  WS-PACKED-X         REDEFINES WS-PACKED                   
                                   PIC  X(009).                           
           03  WS-PACKED-V9        REDEFINES WS-PACKED                   
                                   PIC  9(016)V9   PACKED-DECIMAL.       
           03  WS-BYTE-MASK        PIC  X(016).                           
           03  WS-BIT-VALUE-REC                    BINARY.               
               05  FILLER          PIC  9(008)     VALUE 32768.           
               05  FILLER          PIC  9(008)     VALUE 16384.           
               05  FILLER          PIC  9(008)     VALUE 8192.           
               05  FILLER          PIC  9(008)     VALUE 4096.           
               05  FILLER          PIC  9(008)     VALUE 2048.           
               05  FILLER          PIC  9(008)     VALUE 1024.           
               05  FILLER          PIC  9(008)     VALUE 512.             
               05  FILLER          PIC  9(008)     VALUE 256.             
               05  FILLER          PIC  9(008)     VALUE 128.             
               05  FILLER          PIC  9(008)     VALUE 64.             
               05  FILLER          PIC  9(008)     VALUE 32.             
               05  FILLER          PIC  9(008)     VALUE 16.             
               05  FILLER          PIC  9(008)     VALUE 8.               
               05  FILLER          PIC  9(008)     VALUE 4.               
               05  FILLER          PIC  9(008)     VALUE 2.               
               05  FILLER          PIC  9(008)     VALUE 1.               
           03  FILLER              REDEFINES WS-BIT-VALUE-REC.           
               05  WS-BIT-VALUE-TBL                                       
                                   OCCURS 16 TIMES                       
                                   INDEXED BY X-WS-BVT,                   
                                              X-WS-BVT-MAX               
                                   PIC  9(008)     BINARY.               
           03  WS-RETURN-CODE      PIC  9(004)     BINARY.               
           03  WS-FWORD            PIC  9(008)     BINARY.               
           03  WS-FWORD-X          REDEFINES WS-FWORD                     
                                   PIC  X(004).                           
           03  FILLER              REDEFINES WS-FWORD.                   
               05  FILLER          PIC  X(002).                           
               05  WS-HWORD        PIC  9(004)     BINARY.               
       LINKAGE SECTION.                                                   
       01  LS-PARM-LIST.                                                 
           03  LS-PARM-LGTH        PIC  9(004)     BINARY.               
           03  LS-PARM-AREA        PIC  X(016).                           
       PROCEDURE DIVISION USING LS-PARM-LIST.                             
       0000-BEGIN.                                                       
      *                                                                   
           MOVE LS-PARM-LGTH           TO WS-FWORD.                       
      *                                                                   
           IF  WS-FWORD < LENGTH OF LS-PARM-AREA                         
               MOVE 4095               TO WS-RETURN-CODE                 
               GO TO 9999-END-PROGRAM                                     
           END-IF.                                                       
      *                                                                   
           MOVE ZERO                   TO WS-RETURN-CODE.                 
           MOVE LS-PARM-AREA           TO WS-BYTE-MASK.                   
           DIVIDE LENGTH OF WS-BIT-VALUE-REC                             
                                       BY LENGTH OF WS-BIT-VALUE-TBL (1) 
                                       GIVING WS-FWORD.                   
           SET  X-WS-BVT-MAX           TO WS-HWORD.                       
           SET  X-WS-BVT               TO 1.                             
           MOVE ZERO                   TO WS-FWORD.                       
           MOVE 1                      TO TALLY.                         
      *                                                                   
           PERFORM UNTIL X-WS-BVT > X-WS-BVT-MAX                         
               IF  WS-BYTE-MASK (TALLY:1) = '1'                           
                   ADD  WS-BIT-VALUE-TBL (X-WS-BVT)                       
                                       TO WS-FWORD                       
               ELSE                                                       
                   IF  WS-BYTE-MASK (TALLY:1) NOT = ZERO                 
                       MOVE ZERO       TO WS-BYTE-MASK (TALLY:1)         
                       MOVE 4          TO WS-RETURN-CODE                 
                   END-IF                                                 
               END-IF                                                     
               ADD  1                  TO TALLY                           
               SET  X-WS-BVT           UP BY 1                           
           END-PERFORM.                                                   
      *                                                                   
           DISPLAY SPACE.                                                 
      *                                                                   
           DISPLAY 'PARM VALUE ===> ', WS-BYTE-MASK.                     
      *                                                                   
           MOVE WS-FWORD               TO WS-DISPLAY.                     
      *                                                                   
           DISPLAY SPACE.                                                 
      *                                                                   
           DISPLAY 'CONVERTED DECIMAL VALUE ===> ', WS-DISPLAY-X (13:).   
      *                                                                   
           DISPLAY SPACE.                                                 
      *                                                                   
           MOVE ZERO                   TO WS-PACKED.                     
           MOVE WS-FWORD-X (3:)        TO WS-PACKED-X (1:2).             
           MOVE WS-PACKED-V9           TO WS-DISPLAY-V9.                 
      *                                                                   
           INSPECT WS-DISPLAY-X        CONVERTING X'FAFBFCFDFEFF'         
                                       TO 'ABCDEF'.                       
      *                                                                   
           DISPLAY 'CONVERTED HEX VALUE ===> ', WS-DISPLAY-X (1:4).       
      *                                                                   
           DISPLAY SPACE.                                                 
      *                                                                   
       9999-END-PROGRAM.                                                 
      *                                                                   
           MOVE WS-RETURN-CODE         TO RETURN-CODE.                   
      *                                                                   
           STOP RUN.                                                     

HTH....

Regards,

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

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Jul 30, 2008 2:26 am
Reply with quote

Hello,

On the "flip side", it you want to find the 1's and 0's for a byte you can use:

Code:

FBYTE W 1 B   
FBIT7 W 1 A   
FBIT6 W 1 A   
FBIT5 W 1 A   
FBIT4 W 1 A   
FBIT3 W 1 A   
FBIT2 W 1 A   
FBIT1 W 1 A   
FBIT0 W 1 A   

DISPLAY FBYTE             
IF FBYTE > 127             
   MOVE '1' TO FBIT7       
   FBYTE = FBYTE - 128     
  ELSE                     
   MOVE '0' TO FBIT7       
END-IF                     
IF FBYTE > 63             
   MOVE '1' TO FBIT6       
   FBYTE = FBYTE - 64     
  ELSE                     
   MOVE '0' TO FBIT6       
END-IF                     
IF FBYTE > 31             
   MOVE '1' TO FBIT5       
   FBYTE = FBYTE - 32     
  ELSE                     
   MOVE '0' TO FBIT5       
END-IF                     
IF FBYTE > 15           
   MOVE '1' TO FBIT4     
   FBYTE = FBYTE - 16   
  ELSE                   
   MOVE '0' TO FBIT4     
END-IF                   
IF FBYTE > 7             
   MOVE '1' TO FBIT3     
   FBYTE = FBYTE - 8     
  ELSE                   
   MOVE '0' TO FBIT3     
END-IF                   
IF FBYTE > 3             
   MOVE '1' TO FBIT2     
   FBYTE = FBYTE - 4     
  ELSE                   
   MOVE '0' TO FBIT2     
END-IF                   
IF FBYTE > 1             
   MOVE '1' TO FBIT1     
   FBYTE = FBYTE - 2     
  ELSE                   
   MOVE '0' TO FBIT1     
END-IF                   
IF FBYTE = 1             
   MOVE '1' TO FBIT0     
  ELSE                   
   MOVE '0' TO FBIT0     
END-IF                   
DISPLAY FBIT7           
DISPLAY FBIT6           
DISPLAY FBIT5           
DISPLAY FBIT4           
DISPLAY FBIT3           
DISPLAY FBIT2           
DISPLAY FBIT1           
DISPLAY FBIT0


This is the easytrieve version, but it is easily converted to cobol.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts Error to read log with rexx CLIST & REXX 11
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
Search our Forums:

Back to Top