Representation of Data (1)

Liam·2024년 11월 19일

[Section 02] representation of data

Understanding Decimal Representation in Computers: Zoned Decimal and Packed Decimal

The way computers store and calculate numbers is crucial, especially in environments like mainframes or embedded systems where decimal formats such as 'Zoned Decimal' and 'Packed Decimal' are often used. In this article, we'll explain the differences between these two formats and provide examples to help you understand their characteristics.

What is Zoned Decimal?

Zoned Decimal is a method of representing decimal numbers by converting each digit into an ASCII or EBCDIC character code. Each position combines a 'zone bit' and a 'digit bit,' making it easier for humans to read. For example, if we represent the number "1234" in Zoned Decimal, it would look like this:

Zoned Decimal Example (using EBCDIC code)

1: F1 (in binary 11110001)

2: F2 (in binary 11110010)

3: F3 (in binary 11110011)

4: F4 (in binary 11110100)

Here, F represents the zone bits, while 1, 2, 3, and 4 are the digit bits. The zone bits are typically fixed as F (1111), and the digit bits represent the value of each digit. Thus, "1234" uses a total of 4 bytes. This type of data storage is easy for humans to interpret.

The key feature of Zoned Decimal is its high readability. Since each digit is clearly represented as a character, it's easy for people to understand when debugging or maintaining the data. However, this comes at the cost of memory efficiency. Since each digit takes up 1 byte, more memory is required.

What is Packed Decimal?

Packed Decimal is a method that compresses numeric data to optimize memory usage. In this format, two digits are stored in a single byte. By using Packed Decimal, you can represent the same number with fewer bytes. For instance, representing the number "213" in Packed Decimal would be as follows:

Packed Decimal Example

21: Stored in the first byte (in binary 00100001)

3: Stored in the second byte, including the sign (for positive numbers, 00111100, where the last 4 bits C indicate a positive value)

Thus, +213 is represented as 00100001 00111100 in binary. The last 4 bits C indicate that the number is positive.

Packed Decimal Example (for -213)

21: Stored in the first byte (in binary 00100001)

3: Stored in the second byte, including the sign (for negative numbers, 00111101, where the last 4 bits D indicate a negative value)

Thus, -213 is represented as 00100001 00111101 in binary. The last 4 bits D indicate that the number is negative.

The main advantage of Packed Decimal is memory efficiency. You can store the same number with fewer bytes, which is beneficial when dealing with large amounts of data. However, since the numbers are stored in a format that is harder for humans to interpret, the readability is lower.

Summary of Differences Between Zoned Decimal and Packed

When to Use Which?

Zoned Decimal is useful in situations where human readability or debugging is important. The high readability makes it easy to understand the state of the numeric data.

Packed Decimal is preferable in environments with limited memory or when handling large amounts of numeric data. The efficient use of memory allows you to store more data.

profile
System Software Engineer

0개의 댓글