COMP | BINARY | Computation
COMP | BINARY | Computation
Tip! COMP applies to the numerical data type.
COMP (also known as BINARY) is a USAGE type that stores 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. The amount of storage occupied by a binary item depends on the number of decimal digits defined in its PICTURE clause.
The table below represents the storage occupied based on the number of digits in the PICTURE clause –
Digits in PICTURE clause | Storage occupied |
---|---|
1 through 4 | 2 bytes (halfword) |
5 through 9 | 4 bytes (full word) |
10 through 18 | 8 bytes (doubleword) |
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 - Declaring, initializing, their usage, and display of COMP variables.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
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.
...
Output -
COMP VARIABLE LENGTH IS: 000000004