View previous topic :: View next topic
|
Author |
Message |
Chetan Kumar
New User
Joined: 03 Dec 2012 Posts: 46 Location: India
|
|
|
|
We all know that PIC S9(4) COMP consumes 2 bytes of memory. But the maximum value it can store (i.e. +32767) and why is it not possible to store greater value then this +32767 ? I mean why not +92767? |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Find out what +92767 is in hex and you'll have the answer to that part.
In COBOL, with a USAGE of COMP/COMP-4/BINARY (they are all the same, just synonyms to the compiler) the maximum value that can be held in a PIC 9(4) field is 9999 and the minimum is zero. In a PIC S9(4) the maximum value is +9999 and the minimum is -9999.
Now, with a USAGE of COMP-5 (native binary) the maximum is 65535 and minimum is zero, and +32767 and -32768 (for unsigned and signed fields respectively).
The effect of COMP-5 can be applied to all binary USAGEs by specifying compiler option TRUNC(BIN). This is a bad thing to do.
So for a COBOL binary the maxima are limited by the PICture. For a native binary, the maxima are limited by the bits, the number of bits in a field being to do with the PICture. A PICture of 1-4 digits is a two-byte field, 5-9 a four-byte field, 10-18 an eight-byte field. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
There is a very good write up in the Enterprise COBOL Language Reference manual on the internal representation of data. If you find and read this material, you will learn much that you need to know about COBOL. |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3076 Location: NYC,USA
|
|
|
|
In computer architecture, 16-bit integers, memory addresses, or other data units are those that are at most 16 bits (2 octets) wide. Also, 16-bit CPU and ALU architectures are those that are based on registers, address buses, or data buses of that size. 16-bit is also a term given to a generation of microcomputers in which 16-bit microprocessors were the norm.
A 16-bit register can store 2 * 16 different values. The signed range of integer values that can be stored in 16 bits is -32,768 through 32,767 (unsigned: 0 through 65,535). Hence, a processor with 16-bit memory addresses can directly access 64 KiB of byte-addressable memory. |
|
Back to top |
|
|
himanshu_pant
New User
Joined: 08 Jul 2014 Posts: 14 Location: India
|
|
|
|
Sign is stored in Most significant bit in case of binary. Hence 15 1s(111 1111 1111 1111) in 2-byte binary make the maximum value = +32767). |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Bearing in mind that the answer to the question is +9999 as the maximum and -9999 as the minimum, I think we're sliding into repetition and unclarity. |
|
Back to top |
|
|
Chetan Kumar
New User
Joined: 03 Dec 2012 Posts: 46 Location: India
|
|
|
|
Himanshu,
I agree with you. As sign is stored in most significant bit it will utilize a bit and rest 15 binary bits leads to a value of 32767. Thank you. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Himanshu: you are giving the maximum possible value. COBOL limits variables to the PICTURE size unless a compiler option is set. Hence the value will be 9999 not 32767 unless the compiler option is changed. |
|
Back to top |
|
|
himanshu_pant
New User
Joined: 08 Jul 2014 Posts: 14 Location: India
|
|
|
|
Sure Robert.
I had also faced this issue a while back. Then I had to include
TRUNC(BIN) cobol compiler option in my program to resolve the problem.
Chetan,
This maximum value only applies to S9(4) COMP variables if the TRUNC(BIN) cobol complier option is in effect. If not, then the value is delimited by the number of 9s in the PICture clause (+9999 max and -9999 min). Bill has also explained everything clearly in his reply (use of COMP-5 as well). |
|
Back to top |
|
|
|