COMP | BINARY | Computation


Tip! COMP is applicable to the Numeric Data type.

COMP (also known as BINARY) is a USAGE type used to store signed decimal numbers in pure binary format. This format is often used for arithmetic operations as it provides efficient storage and fast computation.

Storage Size and Format -

COMP USAGE is a binary representation of data stores in pure binary format. The amount of storage occupied by a binary item depends on the number of decimal digits defined in its PICTURE clause.

The below table represents the storage occupied based on the number of digits in the PICTURE clause –

Digits in PICTURE clauseStorage occupied
1 through 42 bytes (halfword)
5 through 94 bytes (full word)
10 through 188 bytes (doubleword)

If the TRUNC compiler option is in effect, BINARY, COMPUTATIONAL, and COMPUTATIONAL-4 variables are affected.

Definition in a COBOL Program -

We use the USAGE IS COMP clause in the DATA DIVISION to define a COMP variable in the COBOL program. For example -

01 WS-BINARY-VAR      PIC 9(5) USAGE IS COMP.

OR

01 WS-BINARY-VAR      PIC 9(5) COMP.

Practical Example -


Scenario - Defining and initializing COMP variables and displaying the length.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMPPROG. 
       AUTHOR. MTH.

       DATA DIVISION. 
       WORKING-STORAGE SECTION.
       01 WS-CVAR                  PIC 9(06) COMP. 

       PROCEDURE DIVISION.

           MOVE 128255             TO WS-CVAR.
           DISPLAY "COMP VARIABLE LENGTH IS: " LENGTH OF WS-CVAR.
		   
           STOP RUN.

Output -

COMP VARIABLE LENGTH IS: 000000004