Page 1 of 1

minus number problem

PostPosted: Wed Feb 24, 2010 12:36 pm
by woaiwanchan
My cobol code is
...
 01 aa pic s9(2).
....
MOVE -21 TO AA.
ADD 10 TO AA
DISPLAY AA.
stop run.


The result is 1j. why?

Re: minus number problem

PostPosted: Wed Feb 24, 2010 6:00 pm
by Robert Sample
Read up in the COBOL manual about the internal representation of numeric data. A PIC S9(2) USAGE DISPLAY variable (which is what you've defined -- the USAGE DISPLAY is implied if you don't specify any other USAGE clause) has two bytes with the sign stored in the second byte as the first four bits (C or x'1100' for positive, D or x'1101' for negative). After you move -21 to AA, the hex value is 'F2D1'. Adding 10 to the value makes the hex value 'F1D1'. When you display the value, a D1 in the collating sequence is a J -- so the display is 1J.