###### Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

## Module 5: Data Division

### Zoned Decimal VS Packed Decimal

Note:- In order to understand this topic, please first refer "Internal Data Represenation in Mainframe" here

A Z/OS system uses 8 -bit bytes (EBCDIC format). Thus, if you view any file in mainframe, its all characters will appear in EBCDIC format

#### Zoned Decimal

• A Zoned decimal representation allocates the first four bits of each byte to a zone (X‘C’ for positive, X‘D’ for negative and X‘F’ for unsigned numeric item) and last four bits of each byte to the value. A zoned decimal value will have the positive/negative zone stored ONLY in the last byte of value. Thus, in case of zoned decimal representation each digit requires a byte
• Note:- Hex value is always represented in format X’value’
• Example:-
• Before understanding above example please refer below table representing how sign value is punched:-
• In above example, since S9999 is used without specifying USAGE clause (thus, by default DISPLAY), the sign will be stored in first nibble of last byte i.e. in our case ‘-4’ will be stored in last byte and ‘1’, ‘2’, ‘3’ will be stored in three separate bytes. Thus, in decimal representation you can see ‘1’,’2’,’3’ are treated as unsigned number and thus, it is stored as ‘F1’,’F2’,F3’ respectively. And since -4 has to be stored in last byte as mentioned above, X’D’ is stored in first nibble and value is stored in last nibble of last byte i.e. X’D4’ represents ‘-4’. When we view these value in mainframe file, it will appear as ‘123M’ because ‘M’ is EBCIDIC representation for ‘-4’

#### Packed Decimal

• A packed decimal representation uses the first four bits of each byte to store one digit and last four bit of each byte(except last byte) to store another digit. The sign is stored in last four bits (X‘C’ for positive, X‘D’ for negative) of last byte. Thus, in case of packed decimal representation it occupies less space compared to zoned decimal representation as each byte(except last byte) can store two digits.
• Example:-