Home > Software > Mainframe > Cobol
Interview Questions   Tutorials   Discussions   Programs   

Cobol - How to display comp3 variables?

asked mar September 10, 2014 11:29 AM  

How to display comp3 variables?


1 Answers

answered By Mswami   0  

Comp-3 fields in COBOL:

Comp-3 stores data in a BCD -- binary coded decimal -- format with the sign after the least significant digit.

COBOL Comp-3 is a binary field type that puts ("packs") two digits into each byte, using a notation called Binary Coded Decimal, or BCD. This halves the storage requirements compared to a character, or COBOL "display", field.

Comp-3 fields are denoted in COBOL with the "usage is" clause after the PIC, like this:

PIC S9(5) usage is computational-3.

However, the "usage is" is not required and seldom used, and "computational-3" is usually abbreviated "comp-3", so you more commonly see: PIC S9(5) comp-3.

The COBOL PIC, or picture, for a comp-3 packed field specifies the number of digits after unpacking. The actual number of bytes occupied in the file is about half that. To calculate the number of bytes from the PIC, add 1 (for the sign) to the total number of digits, divide by 2, and round up if necessary.

For example:

PIC S9(7) COMP-3.     Byte size = (7 + 1) / 2 = 4

PIC S9(5)V99 COMP-3.  Byte size = (5 + 2 + 1) / 2 = 4

PIC S9(6) COMP-3.     Byte size = (6 + 1) / 2 = 3.5, rounded to 4

Comp-3 fields reserve a nybble for the sign, even for "unsigned" values, so the following fields are still 4 bytes:

PIC 9(7) COMP-3.     Byte size = (7 + 1) / 2 = 4

PIC 9(6) COMP-3.     Byte size = (6 + 1) / 2 = 3.5, rounded to 4


Lets look at some examples of how comp-3 data is stored. The left column in the table below is the decimal value being stored, and the right column is the hexadecimal value you will see in the file:

Value  Comp-3, hex
   +0           0C
   +1           1C
  +12        01 2C 
 +123        12 3C
+1234     01 23 4C
   -1           1D
-1234     01 23 4D

Each underlined value above represents one byte, in hexadecimal (hex) form. We have only used as many bytes as needed to store the value shown on the left.

When you "unpack" a packed, or comp-3, field, the size of the field will double. This will cause all fields following it to shift down. If the field is in a redefined area, it will likely no longer fit in the allocated space, and the original field it redefined will have to be modified, or filler will have to be added, to accommodate the larger unpacked field. Just a few comp-3 fields can make for a messy situation, affecting many other fields, and even other records if the file contains multiple record types.

   add comment

Your answer

Join with account you already have



Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!