Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL BINARY vs PACKED-DECIMAL

Intermediate 🕑 12 min read 👁 0 views

COBOL BINARY vs PACKED-DECIMAL

Understanding computational field types for performance and storage.

PACKED-DECIMAL (COMP-3)

```cobol 01 WS-PACKED PIC S9(7)V99 COMP-3. ```

  • Two digits per byte (plus sign nibble)
  • 9(7)V99 = 5 bytes
  • Efficient for decimal arithmetic

BINARY (COMP/COMP-4)

```cobol 01 WS-BINARY PIC S9(9) COMP. ```

  • Hardware binary format
  • 1-4 digits: 2 bytes (halfword)
  • 5-9 digits: 4 bytes (fullword)
  • 10-18 digits: 8 bytes (doubleword)

COMP-5 (Native Binary)

```cobol 01 WS-NATIVE PIC S9(9) COMP-5. ```

  • Uses full range of binary storage
  • May exceed PIC specification

When to Use What

  • PACKED: Financial calculations, most business data
  • BINARY: Counters, indexes, system interfaces
  • COMP-5: C/system interfaces, maximum range

Conversions

Converting between types incurs overhead; keep consistent types in calculations.

Code Example

       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMP-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-DISPLAY PIC 9(7) VALUE 1234567.
       01 WS-PACKED PIC 9(7) COMP-3 VALUE 1234567.
       01 WS-BINARY PIC 9(7) COMP VALUE 1234567.
       01 WS-COMP5 PIC 9(7) COMP-5 VALUE 1234567.
       01 WS-RESULT PIC 9(10).
       PROCEDURE DIVISION.
           DISPLAY 'Display bytes: ' LENGTH OF WS-DISPLAY
           DISPLAY 'Packed bytes: ' LENGTH OF WS-PACKED
           DISPLAY 'Binary bytes: ' LENGTH OF WS-BINARY
           
      * Packed is efficient for decimal math
           COMPUTE WS-RESULT = WS-PACKED * 100
           
      * Binary is efficient for counters
           ADD 1 TO WS-BINARY
           
           STOP RUN.