Welcome to the new home of PowerWire – PowerWire.uk

Update your bookmarks now!

Informing the IBM Community

Hackers Delight

Hackers Delight – Part 1

5
(2)

What are Packed Fields?

Packed fields are an essential data structure in RPG programming, specifically designed to efficiently store numeric values.

This chapter explores the definition, purpose, structure, and advantages of packed fields, along with practical examples to illustrate their use.

Definition of Packed Fields

Packed fields are a compact numeric storage format based on Binary Coded Decimal (BCD).

Each decimal digit is stored in a nibble (4 bits), allowing two digits to be stored in a single byte.

The sign of the number is stored in the last nibble, using specific hexadecimal values to denote positive (F) and negative (D) numbers.

This structure makes packed fields highly efficient for storing decimal numbers while maintaining precision and ease of manipulation in RPG applications.

Structure of Packed Fields

Binary Coded Decimal (BCD)

Each decimal digit is converted into its binary equivalent and stored in 4 bits.

Example: The number 123 is represented as:

  • Decimal digits: 1, 2, 3
  • BCD representation: 0001 0010 0011 1111 (including the closing nibble for the sign).

Nibble Representation

  • A nibble is a group of 4 bits. Packed fields use nibbles to store decimal digits.
  • Two nibbles form a byte, meaning a byte can store two decimal digits.

Sign Storage

The last nibble indicates the sign of the number.

  • F (positive): 123 is stored as 12 3F.
  • D (negative): -123 is stored as 12 3D.

Example Representations

Consider the number 123.45 with a packed field definition of packed(9:2):

  • Length: 9 digits, including 2 decimal places. This requires ceil(9/2) = 5 bytes.
  • Packed Representation – Decimal digits: 0012345 (padded with leading zeros to fit 9 digits).
  • Packed Representation – Packed storage: 00 12 34 5F (5 bytes).

Similarly, for -987.65:

  • Decimal digits: 0098765 (padded with leading zeros to fit 9 digits).
  • Packed storage: 00 98 76 5D (5 bytes).

Advantages of Packed Fields

  • Efficiency: Packed fields use half the storage space compared to character-based numeric formats. For instance, a 9-digit number requires only 5 bytes in packed format, compared to 9 bytes in character format.
  • Precision: Packed fields support exact decimal representation, making them ideal for financial and accounting applications.
  • Compatibility: Native support in RPG for arithmetic operations ensures seamless processing of numeric values without conversion overhead.
  • Reduced Storage Requirements: Packed fields allow systems to store more data in less space, improving overall memory usage and performance.

Real-World Application

Packed fields are extensively used in RPG for scenarios involving:

  • Financial calculations requiring high precision.
  • Integration with legacy systems that rely on packed data formats.
  • Database storage optimization, especially for numeric fields.

Packed fields are a cornerstone of efficient numeric processing in RPG.

Their compact structure, precision, and storage efficiency make them indispensable for developers working with data-intensive applications.

In the following chapters, we will explore how bit operations and other techniques enable advanced manipulation and conversions involving packed fields.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.


Comments

2 responses to “Hackers Delight – Part 1”

  1. Glenn Gundermann avatar
    Glenn Gundermann

    The examples don’t look correct to me. You write “padded with leading zeros to fit 9 digits” and yet you only show 7 digits. You write “00 12 34 5F (5 bytes)” but this is only 4 bytes.

  2. Glenn Gundermann avatar
    Glenn Gundermann

    You wrote the advantages of using a packed field but you didn’t include the disadvantages, or when it’s best to use them or not.

Leave a Reply

Your email address will not be published. Required fields are marked *