MainframeMaster

COBOL Tutorial

STANDARD-BINARY - Binary Integers

Progress0 of 0 lessons

When to use binary vs decimal

Use BINARY/COMP for counters, indexes, and flags. Use PACKED-DECIMAL/DISPLAY for currency and precise decimal math.

Typical Declarations and Sizes

cobol
1
2
3
01 I16 PIC S9(4) BINARY. *> ~2 bytes 01 I32 PIC S9(9) BINARY. *> ~4 bytes 01 I64 PIC S9(18) BINARY. *> ~8 bytes

Compiler options can influence exact mapping. Check documentation.

Range Guide (Typical)

DigitsBytesSigned Range (approx)
1–42-32768 .. 32767
5–94-2,147,483,648 .. 2,147,483,647
10–188~±9.22e18

Signedness and Alignment

cobol
1
2
01 COUNT PIC 9(5) BINARY. *> Unsigned 01 OFFSET PIC S9(5) BINARY. *> Signed

Alignment affects how fields are placed in memory; aligned fields may be faster to access.

COMP/COMP-5 Notes

  • COMP often maps to BINARY; COMP-5 may indicate native machine binary sizes without decimal truncation rules (compiler-dependent)
  • Be explicit with BINARY for clarity

Best Practices and Common Mistakes

Best Practices

  • Pick the smallest type that safely fits the range
  • Use decimal types for financial data
  • Document assumptions when writing binary to external files

Common Mistakes

MistakeProblemFix
Using BINARY for moneyRounding/inexact valuesUse PACKED-DECIMAL
OverflowNegative wrap/abendUse a wider type; validate inputs

Quick Reference

UsageMeaningExample
BINARYBinary integerPIC S9(9) BINARY
COMPCompiler-mapped binaryPIC 9(4) COMP
COMP-5Native binary (compiler-specific)PIC S9(9) COMP-5

Test Your Knowledge

1. Which usages indicate binary integers?

  • DISPLAY
  • BINARY/COMP
  • PACKED-DECIMAL
  • NATIONAL

2. Best for money?

  • BINARY
  • PACKED-DECIMAL
  • FLOAT
  • NATIONAL

3. Why alignment matters?

  • Affects performance/storage
  • Affects color
  • Affects screen size
  • No reason

Frequently Asked Questions

Related Pages