MainframeMaster

COBOL Tutorial

COBOL BINARY-SHORT

The BINARY-SHORT data type in COBOL represents a specialized 16-bit integer format designed for memory-efficient storage and seamless interoperability with C programming language applications, specifically matching the characteristics of C's short data type. This compact data type is essential for applications requiring optimal memory utilization, embedded systems programming, network protocol implementation, and scenarios where thousands or millions of small integer values must be stored efficiently while maintaining exact C language compatibility for mixed-language programming environments.

Understanding BINARY-SHORT

BINARY-SHORT provides a fixed 16-bit integer representation (2 bytes) that matches the C short data type across all platforms. This compact format is ideal for applications where memory conservation is critical while still providing a reasonable range of integer values for most business and technical applications.

Key Characteristics:

  • Fixed 16-bit (2 bytes) storage on all platforms
  • Range: -32,768 to 32,767 (signed)
  • Range: 0 to 65,535 (unsigned)
  • 50% memory savings compared to BINARY-LONG
  • Perfect for counters, flags, and small identifiers
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
DATA DIVISION. WORKING-STORAGE SECTION. 01 MEMORY-EFFICIENT-COUNTERS. 05 PAGE-NUMBER USAGE BINARY-SHORT. 05 LINE-COUNT USAGE BINARY-SHORT. 05 COLUMN-POSITION USAGE BINARY-SHORT. 05 ERROR-COUNT USAGE BINARY-SHORT. 01 NETWORK-PROTOCOL-DATA. 05 PORT-NUMBER USAGE BINARY-SHORT. 05 PACKET-SIZE USAGE BINARY-SHORT. 05 SEQUENCE-NUMBER USAGE BINARY-SHORT. 05 CHECKSUM-VALUE USAGE BINARY-SHORT. 01 ARRAY-INDICES. 05 ROW-INDEX USAGE BINARY-SHORT. 05 COLUMN-INDEX USAGE BINARY-SHORT. 05 TABLE-SIZE USAGE BINARY-SHORT VALUE 1000. PROCEDURE DIVISION. MOVE 80 TO PORT-NUMBER. MOVE 1024 TO PACKET-SIZE. MOVE 1 TO PAGE-NUMBER.

Memory-Efficient Data Structures

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
*> Large arrays with memory optimization DATA DIVISION. WORKING-STORAGE SECTION. 01 INVENTORY-SYSTEM. 05 MAX-ITEMS USAGE BINARY-SHORT VALUE 10000. 05 ITEM-TABLE OCCURS 10000 TIMES. 10 ITEM-ID USAGE BINARY-SHORT. 10 QUANTITY USAGE BINARY-SHORT. 10 REORDER-LEVEL USAGE BINARY-SHORT. 10 SUPPLIER-CODE USAGE BINARY-SHORT. 01 STATISTICS-COUNTERS. 05 DAILY-SALES USAGE BINARY-SHORT OCCURS 365 TIMES. 05 MONTHLY-TOTALS USAGE BINARY-SHORT OCCURS 12 TIMES. 05 HOURLY-COUNTS USAGE BINARY-SHORT OCCURS 24 TIMES. 01 COORDINATE-SYSTEM. 05 POINT-ARRAY OCCURS 5000 TIMES. 10 X-COORDINATE USAGE BINARY-SHORT. 10 Y-COORDINATE USAGE BINARY-SHORT. 10 Z-COORDINATE USAGE BINARY-SHORT. PROCEDURE DIVISION. INITIALIZE-INVENTORY. PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > MAX-ITEMS MOVE WS-INDEX TO ITEM-ID(WS-INDEX) MOVE 0 TO QUANTITY(WS-INDEX) MOVE 10 TO REORDER-LEVEL(WS-INDEX) MOVE 100 TO SUPPLIER-CODE(WS-INDEX) END-PERFORM. UPDATE-STATISTICS. *> Update daily sales counter ADD 1 TO DAILY-SALES(WS-DAY-OF-YEAR). *> Update hourly count ADD 1 TO HOURLY-COUNTS(WS-CURRENT-HOUR). *> Calculate monthly total COMPUTE MONTHLY-TOTALS(WS-CURRENT-MONTH) = MONTHLY-TOTALS(WS-CURRENT-MONTH) + WS-SALE-AMOUNT. PROCESS-COORDINATES. PERFORM VARYING WS-POINT FROM 1 BY 1 UNTIL WS-POINT > 5000 COMPUTE X-COORDINATE(WS-POINT) = WS-POINT * 10 COMPUTE Y-COORDINATE(WS-POINT) = WS-POINT * 20 COMPUTE Z-COORDINATE(WS-POINT) = WS-POINT * 5 IF X-COORDINATE(WS-POINT) > 30000 DISPLAY "Warning: X coordinate approaching limit" END-IF END-PERFORM.

Network and Protocol Programming

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
*> Network protocol implementation DATA DIVISION. WORKING-STORAGE SECTION. 01 TCP-HEADER. 05 SOURCE-PORT USAGE BINARY-SHORT. 05 DEST-PORT USAGE BINARY-SHORT. 05 SEQUENCE-NUM USAGE BINARY-LONG. 05 ACK-NUM USAGE BINARY-LONG. 05 WINDOW-SIZE USAGE BINARY-SHORT. 05 CHECKSUM USAGE BINARY-SHORT. 05 URGENT-POINTER USAGE BINARY-SHORT. 01 HTTP-RESPONSE. 05 STATUS-CODE USAGE BINARY-SHORT. 05 CONTENT-LENGTH USAGE BINARY-LONG. 05 CONNECTION-ID USAGE BINARY-SHORT. 01 PROTOCOL-CONSTANTS. 05 HTTP-PORT USAGE BINARY-SHORT VALUE 80. 05 HTTPS-PORT USAGE BINARY-SHORT VALUE 443. 05 FTP-PORT USAGE BINARY-SHORT VALUE 21. 05 SSH-PORT USAGE BINARY-SHORT VALUE 22. 05 TELNET-PORT USAGE BINARY-SHORT VALUE 23. PROCEDURE DIVISION. BUILD-TCP-HEADER. MOVE 8080 TO SOURCE-PORT. MOVE HTTP-PORT TO DEST-PORT. MOVE 1024 TO WINDOW-SIZE. MOVE 0 TO CHECKSUM. *> Calculate checksum CALL "calculate_checksum" USING BY REFERENCE TCP-HEADER BY REFERENCE CHECKSUM END-CALL. PROCESS-HTTP-REQUEST. EVALUATE STATUS-CODE WHEN 200 DISPLAY "OK - Request successful" WHEN 404 DISPLAY "Not Found" WHEN 500 DISPLAY "Internal Server Error" WHEN OTHER DISPLAY "HTTP Status: " STATUS-CODE END-EVALUATE. VALIDATE-PORT-RANGE. IF SOURCE-PORT < 1024 DISPLAY "Warning: Using privileged port " SOURCE-PORT END-IF. IF DEST-PORT > 65535 DISPLAY "Error: Invalid port number " DEST-PORT MOVE 1 TO WS-ERROR-FLAG END-IF.

Best Practices

  • Use for memory-critical applications with large arrays
  • Perfect for counters, indices, and small identifiers
  • Ideal for network port numbers and protocol fields
  • Always validate values are within 16-bit range
  • Consider signed vs unsigned based on data requirements

Memory Savings Example

Data TypeSize per Element10,000 ElementsMemory Usage
BINARY-SHORT2 bytes20,000 bytes19.5 KB
BINARY-LONG4 bytes40,000 bytes39.1 KB
PIC 9(5)5 bytes50,000 bytes48.8 KB

Frequently Asked Questions

Q: When should I use BINARY-SHORT instead of BINARY-LONG?

Use BINARY-SHORT when values will never exceed 32,767 (signed) or 65,535 (unsigned) and memory conservation is important. It's perfect for counters, array indices, port numbers, and flags. Use BINARY-LONG when you need larger values or aren't concerned about memory usage.

Q: What happens if I exceed the BINARY-SHORT range?

Exceeding the range can cause overflow, leading to wraparound behavior or runtime errors depending on your COBOL compiler and runtime settings. Always validate input values and consider using BINARY-LONG or multiple fields if larger values are possible.

Q: Is BINARY-SHORT portable across different platforms?

Yes, BINARY-SHORT is consistently 16 bits (2 bytes) across all platforms, making it highly portable. This fixed size is one of its advantages over some other data types that may vary by platform.