minus number problem



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

minus number problem

Postby woaiwanchan » Wed Feb 24, 2010 12:36 pm

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?
woaiwanchan
 
Posts: 6
Joined: Wed Feb 24, 2010 10:06 am
Has thanked: 0 time
Been thanked: 0 time

Re: minus number problem

Postby Robert Sample » Wed Feb 24, 2010 6:00 pm

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post