How to resolve the algorithm Bitwise operations step by step in the COBOL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitwise operations step by step in the COBOL programming language

Table of Contents

Problem Statement

Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate. All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer. If any operation is not available in your language, note it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitwise operations step by step in the COBOL programming language

Source code in the cobol programming language

       IDENTIFICATION DIVISION.
       PROGRAM-ID. bitwise-ops.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       01  a                       PIC 1(32) USAGE BIT.
       01  b                       PIC 1(32) USAGE BIT.
       01  result                  PIC 1(32) USAGE BIT.
       01  result-disp             REDEFINES result
                                   PIC S9(9) USAGE COMPUTATIONAL.
       LINKAGE SECTION.
       01  a-int                   USAGE BINARY-LONG.
       01  b-int                   USAGE BINARY-LONG.

       PROCEDURE DIVISION USING a-int, b-int.
           MOVE FUNCTION BOOLEAN-OF-INTEGER(a-int, 32) TO a
           MOVE FUNCTION BOOLEAN-OF-INTEGER(b-int, 32) TO b

           COMPUTE result = a B-AND b
           DISPLAY "a and b is " result-disp

           COMPUTE result = a B-OR b
           DISPLAY "a or b is " result-disp

           COMPUTE result = B-NOT a
           DISPLAY "Not a is " result-disp

           COMPUTE result = a B-XOR b
           DISPLAY "a exclusive-or b is " result-disp

      *>   More complex operations can be constructed from these.

           COMPUTE result = B-NOT (a B-XOR b)
           DISPLAY "Logical equivalence of a and b is " result-disp

           COMPUTE result = (B-NOT a) B-AND b
           DISPLAY "Logical implication of a and b is " result-disp

      *>   Shift and rotation operators were only added in COBOL 2023.

           COMPUTE result = a B-SHIFT-L b
           DISPLAY "a shifted left by b is " result-disp

           COMPUTE result = b B-SHIFT-R a
           DISPLAY "b shifted right by a is " result-disp

           COMPUTE result = a B-SHIFT-LC b
           DISPLAY "a rotated left by b is " result-disp

           COMPUTE result = b B-SHIFT-RC a
           DISPLAY "b rotated right by a is " result-disp

           GOBACK.

       END PROGRAM bitwise-ops.


       IDENTIFICATION DIVISION.
       PROGRAM-ID. mf-bitwise-ops.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       01  result                  USAGE BINARY-LONG.
       78  arg-len                 VALUE LENGTH OF result.

       LINKAGE SECTION.
       01  a                       USAGE BINARY-LONG.
       01  b                       USAGE BINARY-LONG.

       PROCEDURE DIVISION USING a, b.
       main-line.
           MOVE b TO result
           CALL "CBL_AND" USING a, result, VALUE arg-len
           DISPLAY "a and b is " result

           MOVE b TO result
           CALL "CBL_OR" USING a, result, VALUE arg-len
           DISPLAY "a or b is " result

           MOVE a TO result
           CALL "CBL_NOT" USING result, VALUE arg-len
           DISPLAY "Not a is " result

           MOVE b TO result
           CALL "CBL_XOR" USING a, result, VALUE arg-len
           DISPLAY "a exclusive-or b is " result

           MOVE b TO result
           CALL "CBL_EQ" USING a, result, VALUE arg-len
           DISPLAY "Logical equivalence of a and b is " result

           MOVE b TO result
           CALL "CBL_IMP" USING a, result, VALUE arg-len
           DISPLAY "Logical implication of a and b is " result

           GOBACK.

       END PROGRAM mf-bitwise-ops.


  

You may also check:How to resolve the algorithm Take notes on the command line step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the AWK programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Swift programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Picat programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the Modula-3 programming language