Data Types in COBOL
Recently I had a very fulfilling interview with CTS. One of the question worth mentioning is regarding COMP-3,
is stored in COMP-3, and I was able to answer the question(thanks to my blogging). But after the interview, whe
questions with my friends, they said questions were tough and also these types of questions were unheard of in So I thought to pen down my knowledge for the benefit of everyone. This article will deal with data types in COBOL particularly COMP,COMP-1,COMP-2,COMP-3 & Zoned Decimal.
COMP
In these types of variable, the values will be converted to its binary equivalent and stored. Fo 123, whose binary equivalent is 1111011. The number will be stored as 1111011.
The COMP(Or Binary) type of variable can be signed or unsigned. If signed Left Most bit(or MS operational sign. The negative number are stored as 2's Complement. The memory available S9(1) - S9(4) ---> 2 Byte(Half Word) S9(5) - S9(9) ---> 4 Byte(Full Word) S9(10) - S9(18) ----> 8 Byte(Double Word) *** 18 is maximum digit that can be used.
On COMP Usage COBOL Programming Guide says - Binary item should be used for index, sub arithmetic operands or results.
COMP-1 & COMP-2
These stores the data as floating point representation following IEEE floating point standard.
between two comes in precision. COMP-1 is single precision and COMP-2 is double precision 56 Bits are used to represent the fractional part.
COMP-1 takes 4 Bytes of which 24 Bits are used to represent fractional part, while COMP-2 t
Note - In my system(IBM Mainframe) I have not seen any usage of COMP-1 or COMP-2(Proba
doesn't follow IEEE Standard). So I don't know when and where should COMP-1 & COMP-2 be
COMP-3
This is the most interesting data type in COBOL, because of the way it is stored. These are als decimal, and reasons will be clear at end of this section. Lets deal with basics first.
Every processor assign 1 Byte(8-Bit) to each character. Now in case of number, a digit can be digit can be packed in one byte, if each are represented in binary form independently. These called Binary Coded Decimal(BCD). By Defination BCD Stores decimal(Base 10) notation and each digit is binary coded. nibble(Least Significant Bit) Following are the examples
maximum by 4 Bits(Binary Equivalent of 9 is 1001). So in One Byte or 8 Bits, 2 Digits can be a
To simplify matter, each digit is stored in a nibble(4 Bit) in binary format. The sign is punche
Value
+0 +1 +12 -1234
COMP-3
0C 1C 01 2C(Sign will always be last nibble) 01 23 4D
Please note in case digits are lesser than number of nibble, then zero is added in MSB.
Apart from this the arithmatic calculation is also interesting for COMP-3. But I am not explain significant position and then added.
reader to find out or better think. One hint though, in case the pic clause is different zeros ar
ZONED DECIMAL
Zoned decimal has always baffled me, due to its unusual name, after all why should some var
Answer lies in Punch Cards. Zoned Decimal were first used in Punched cards with 80 Column were used.
rows(12,11,0) called Zone and 0-9 called decimal.To determine or punch negative number th
Though we have moved away from punched card, but the concept of zoned decimal remain s FFFF meaning Positive) and lower bit or last 4 bit will represent the number in binary form.
decimal, each digit is stored in One Byte, but the higher Bit(4 Bit or Left Most Nibble) will repr
Though Signs are coded with every digit(or Byte) but the actual sign will be stored in last byte Zoned Decimal).
representing the sign and lower bit representing the number.(In COBOL any number with USA