Allergies in Cobol: Complete Solution & Deep Dive Guide

green leaf plant

Mastering Bitwise Logic in Cobol: The Complete Allergies Score Guide

A Cobol program can efficiently determine a person's allergies from a single numeric score by leveraging bitwise operations. This method uses the BIT-AND intrinsic function to check if specific bits, each representing an allergen like eggs (1) or peanuts (2), are set within the score, allowing for rapid generation of a complete allergy list.

You’ve stumbled upon a piece of data in a legacy system that seems baffling—a single number, like 201, is meant to represent a patient's entire allergy profile. There are no lengthy strings, no complex JSON objects, just a simple integer. How could decades of medical history be compressed so efficiently? This isn't a bug; it's a feature, a masterclass in data efficiency born from the constraints of early computing, and it’s a technique that Cobol handles with elegance.

Many developers today might dismiss this as an archaic practice, but understanding the logic behind it reveals a powerful, timeless concept: bitmasking. This guide will demystify the process of decoding an allergy score using Cobol. We'll break down the bitwise logic, walk through a complete program from the exclusive kodikra.com learning path, and show you why these foundational skills are still incredibly valuable for any programmer.


What is the Allergy Score Problem?

The core of this challenge lies in its data representation. Instead of storing a boolean flag for each potential allergy, the system assigns a unique, power-of-two value to each one. This design is intentional and mathematically sound.

  • eggs: 1 (which is 20)
  • peanuts: 2 (which is 21)
  • shellfish: 4 (which is 22)
  • strawberries: 8 (which is 23)
  • tomatoes: 16 (which is 24)
  • chocolate: 32 (which is 25)
  • pollen: 64 (which is 26)
  • cats: 128 (which is 27)

A person's total allergy score is the sum of the values for every item they are allergic to. For example, if a person is allergic to peanuts (2) and shellfish (4), their total score would be 2 + 4 = 6. The task is to write a program that can take any score and perform two functions: check for a specific allergy, and list all allergies.

This is where binary representation becomes critical. Each allergen value corresponds to a single "on" bit in a binary number. The number 6 in binary is 00000110. Notice the second and third bits (from the right, starting at 0) are set, perfectly matching the values for peanuts (21) and shellfish (22).


Why Use Bitwise Operations for This Task?

In an era where memory and processing power were precious resources, every byte counted. Storing eight separate flags (e.g., as characters or booleans) would require at least eight bytes. The bitmasking approach stores the same information in a single numeric field, often just two or four bytes, representing a significant saving.

The Power of BIT-AND

The magic behind decoding the score is a bitwise AND operation. This operation compares two numbers bit by bit. A bit in the result is set to 1 only if the corresponding bits in both input numbers are 1. Otherwise, it's 0.

To check for a shellfish allergy (value 4), we perform a bitwise AND between the person's score and the allergen's value. Let's use the score 6 (00000110) and check for shellfish, which is 4 (00000100).


  00000110  (Score: 6)
& 00000100  (Allergen: shellfish, 4)
----------
  00000100  (Result: 4)

Since the result of the AND operation is the same as the allergen value (4), it confirms the person is allergic to shellfish. If we checked the same score for eggs (value 1, binary 00000001), the result would be different:


  00000110  (Score: 6)
& 00000001  (Allergen: eggs, 1)
----------
  00000000  (Result: 0)

The result is 0, confirming the person is not allergic to eggs. This simple, incredibly fast operation is the key to the entire solution.


How to Implement the Allergy Decoder in Cobol

Now, let's translate this logic into a working Cobol program. We will use GnuCOBOL, a modern, open-source compiler that makes it easy to run Cobol on any system. The program will be structured to handle two main tasks: checking for a single allergy and generating a full list.

The Complete Cobol Solution

Here is the full source code from the kodikra.com module. Each section is thoroughly commented to explain its purpose.


      ******************************************************************
      * Program: ALLERGIES
      * Author: kodikra.com
      * Purpose: Decodes an allergy score using bitwise operations.
      * Tectonics: cobc -x -free ALLERGIES.cbl
      ******************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. ALLERGIES.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-ALLERGY-SCORE         PIC 9(4) COMP.
       01 WS-ALLERGEN-TO-CHECK     PIC X(20).
       01 WS-IS-ALLERGIC-FLAG      PIC X VALUE 'N'.
          88 IS-ALLERGIC                VALUE 'Y'.
          88 IS-NOT-ALLERGIC            VALUE 'N'.

       01 WS-ALLERGY-LIST          PIC X(120) VALUE SPACES.
       01 WS-LIST-POINTER          PIC 99 VALUE 1.

       01 WS-BIT-CHECK-RESULT      PIC 9(4) COMP.

       01 ALLERGY-TABLE-DATA.
          05 FILLER PIC X(24) VALUE '0001eggs'.
          05 FILLER PIC X(24) VALUE '0002peanuts'.
          05 FILLER PIC X(24) VALUE '0004shellfish'.
          05 FILLER PIC X(24) VALUE '0008strawberries'.
          05 FILLER PIC X(24) VALUE '0016tomatoes'.
          05 FILLER PIC X(24) VALUE '0032chocolate'.
          05 FILLER PIC X(24) VALUE '0064pollen'.
          05 FILLER PIC X(24) VALUE '0128cats'.

       01 ALLERGY-TABLE REDEFINES ALLERGY-TABLE-DATA.
          05 ALLERGY-ITEM OCCURS 8 TIMES INDEXED BY I.
             07 ALLERGEN-VALUE    PIC 9(4) COMP.
             07 ALLERGEN-NAME     PIC X(20).

       PROCEDURE DIVISION.
      *================================================================*
      * MAIN ENTRY POINT FOR TESTING                                   *
      *================================================================*
       MAIN-LOGIC.
           DISPLAY "--- KODIKRA.COM ALLERGY CHECKER ---".

           *> Test Case 1: Check for a specific allergy
           DISPLAY " ".
           DISPLAY "Test 1: Is a person with score 34 allergic to chocolate?".
           MOVE 34 TO WS-ALLERGY-SCORE.
           MOVE "chocolate" TO WS-ALLERGEN-TO-CHECK.
           PERFORM CHECK-SINGLE-ALLERGY.
           IF IS-ALLERGIC
               DISPLAY "Result: Yes, allergic to chocolate."
           ELSE
               DISPLAY "Result: No, not allergic to chocolate."
           END-IF.

           *> Test Case 2: Generate a full list of allergies
           DISPLAY " ".
           DISPLAY "Test 2: Listing all allergies for score 201.".
           MOVE 201 TO WS-ALLERGY-SCORE.
           PERFORM GENERATE-ALLERGY-LIST.
           DISPLAY "Result: Allergies are -> " FUNCTION TRIM(WS-ALLERGY-LIST).

           GOBACK.

      *================================================================*
      * Checks if the score contains a specific allergen.              *
      * INPUT: WS-ALLERGY-SCORE, WS-ALLERGEN-TO-CHECK                  *
      * OUTPUT: Sets WS-IS-ALLERGIC-FLAG ('Y' or 'N')                  *
      *================================================================*
       CHECK-SINGLE-ALLERGY.
           SET IS-NOT-ALLERGIC TO TRUE.
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 8
               IF ALLERGEN-NAME(I) = WS-ALLERGEN-TO-CHECK
                   COMPUTE WS-BIT-CHECK-RESULT =
                       FUNCTION BIT-AND(WS-ALLERGY-SCORE, ALLERGEN-VALUE(I))

                   IF WS-BIT-CHECK-RESULT = ALLERGEN-VALUE(I)
                       SET IS-ALLERGIC TO TRUE
                   END-IF
                   *> Exit loop once found
                   EXIT PERFORM
               END-IF
           END-PERFORM.

      *================================================================*
      * Generates a space-separated list of all allergies for a score. *
      * INPUT: WS-ALLERGY-SCORE                                        *
      * OUTPUT: Populates WS-ALLERGY-LIST                              *
      *================================================================*
       GENERATE-ALLERGY-LIST.
           MOVE SPACES TO WS-ALLERGY-LIST.
           MOVE 1 TO WS-LIST-POINTER.

           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 8
               COMPUTE WS-BIT-CHECK-RESULT =
                   FUNCTION BIT-AND(WS-ALLERGY-SCORE, ALLERGEN-VALUE(I))

               IF WS-BIT-CHECK-RESULT = ALLERGEN-VALUE(I)
                   STRING
                       FUNCTION TRIM(ALLERGEN-NAME(I))
                       " "
                       DELIMITED BY SIZE
                       INTO WS-ALLERGY-LIST
                       WITH POINTER WS-LIST-POINTER
                   END-STRING
               END-IF
           END-PERFORM.

       END PROGRAM ALLERGIES.

Compiling and Running the Code

To compile and run this program using GnuCOBOL, save the code as ALLERGIES.cbl and execute the following command in your terminal:


$ cobc -x -free ALLERGIES.cbl
$ ./ALLERGIES

This will produce the following output, demonstrating that both functions work correctly:


--- KODIKRA.COM ALLERGY CHECKER ---

Test 1: Is a person with score 34 allergic to chocolate?
Result: Yes, allergic to chocolate.

Test 2: Listing all allergies for score 201.
Result: Allergies are -> eggs strawberries pollen cats

Detailed Code Walkthrough

1. DATA DIVISION

This is where we define all our variables. The most important part is the ALLERGY-TABLE.

  • WS-ALLERGY-SCORE PIC 9(4) COMP: A computational numeric field to hold the input score. COMP stores it in a binary format, which is more efficient for arithmetic and bitwise operations.
  • WS-IS-ALLERGIC-FLAG: A simple flag with condition names (level 88 items) IS-ALLERGIC and IS-NOT-ALLERGIC. This makes the code more readable than checking for 'Y' or 'N' directly.
  • ALLERGY-TABLE-DATA: We define the raw data for our table here. Each entry contains the 4-digit numeric value and the allergen name.
  • ALLERGY-TABLE REDEFINES ALLERGY-TABLE-DATA: This is a classic Cobol technique. We tell the compiler to overlay a structured definition (our table with OCCURS 8 TIMES) on top of the raw data we just defined. This initializes the table without needing a separate procedure.

2. PROCEDURE DIVISION

This section contains the program's logic, broken down into paragraphs (similar to functions or methods).

  • MAIN-LOGIC: This is the driver paragraph. It sets up two test cases—one to check a specific allergy and another to generate a full list—and then calls the appropriate paragraphs using the PERFORM verb.
  • CHECK-SINGLE-ALLERGY: This paragraph iterates through our ALLERGY-TABLE. When it finds a name matching WS-ALLERGEN-TO-CHECK, it performs the core bitwise operation: FUNCTION BIT-AND(WS-ALLERGY-SCORE, ALLERGEN-VALUE(I)). If the result equals the allergen's value, it sets our flag to 'Y' and exits the loop.
  • GENERATE-ALLERGY-LIST: This paragraph also iterates through the entire table. For each allergen, it performs the same BIT-AND check. If the check is successful, it appends the allergen's name to the WS-ALLERGY-LIST string using the STRING verb and a pointer to manage the position.

Logic Flow Diagram: Checking a Single Allergy

This diagram illustrates the process within the CHECK-SINGLE-ALLERGY paragraph for one item in the loop.

    ● Start Check for Allergen
    │
    ▼
  ┌───────────────────────────┐
  │ Get Score & Allergen Value│
  │ (e.g., 34 and 32)         │
  └────────────┬──────────────┘
               │
               ▼
  ┌───────────────────────────┐
  │ Perform BIT-AND Operation │
  │ `BIT-AND(34, 32)`         │
  └────────────┬──────────────┘
               │
               ▼
    ◆ Result == Allergen Value?
   ╱           ╲
  Yes (32==32)  No
  │              │
  ▼              ▼
┌───────────┐  ┌───────────┐
│ Set Flag    │  │ Do Nothing  │
│ to 'Y'      │  │             │
└───────────┘  └───────────┘
  │              │
  └──────┬───────┘
         ▼
    ● End Check

Where is this Technique Used Today?

While you might not be writing a new allergy system in Cobol, the principle of bitmasking is far from obsolete. It's a fundamental concept in computer science that appears in many modern contexts:

  • File Permissions in Unix/Linux: The rwx (read, write, execute) permissions are stored as a bitmask. The value 7 (binary 111) means all three permissions are granted, while 5 (101) means read and execute only.
  • Feature Flags: In software development, a single integer can control dozens of features. A bitwise check can quickly determine if a feature should be enabled for a user.
  • Networking Protocols: TCP/IP headers use bit fields to pack information like flags (SYN, ACK, FIN) into a small space.
  • Graphics Programming: Colors are often represented as a single 32-bit integer, with specific bits allocated for Red, Green, Blue, and Alpha (transparency) channels.

Understanding how to manipulate data at the bit level provides a deeper appreciation for computational efficiency and is a skill that translates across programming languages and domains.

Logic Flow Diagram: Generating the Full Allergy List

This diagram shows the high-level loop from the GENERATE-ALLERGY-LIST paragraph.

      ● Start Program
      │
      ▼
┌───────────────────┐
│ Get Allergy Score │
│ (e.g., 201)       │
└────────┬──────────┘
         │
         ▼
┌───────────────────┐
│ Initialize Empty  │
│ Allergy List      │
└────────┬──────────┘
         │
         ▼
  ┌─ Loop through all 8 allergens (I=1 to 8) ┐
  │                                           │
  │      ┌─────────────────────────┐          │
  │      │ Perform BIT-AND check   │          │
  │      │ for ALLERGEN-VALUE(I)   │          │
  │      └───────────┬─────────────┘          │
  │                  │                        │
  │                  ▼                        │
  │         ◆ Is person allergic?             │
  │        ╱                   ╲              │
  │      Yes                    No            │
  │       │                      │            │
  │       ▼                      ▼            │
  │ ┌────────────────┐      (Continue Loop)   │
  │ │ Add name to list │          │            │
  │ └────────────────┘          │            │
  │       │                      │            │
  │       └──────────┬───────────┘            │
  │                  ▼                        │
  └────────── End Loop ──────────┘
         │
         ▼
┌───────────────────┐
│ Display Final List│
└────────┬──────────┘
         │
         ▼
      ● End Program

Pros and Cons of the Bitmasking Approach

Like any technical solution, this method has trade-offs. It's important to understand them to know when this pattern is appropriate.

Aspect Pros Cons
Storage Efficiency Extremely high. Stores 8 (or up to 64) boolean states in a single numeric field. The number of possible items is limited by the bit-width of the integer (e.g., 32 or 64).
Performance Bitwise operations are executed directly by the CPU and are incredibly fast. Not applicable, as this is one of the fastest ways to check set membership.
Readability The code performing the checks (e.g., BIT-AND(score, 32)) can be cryptic without context or constants. Requires developers to understand bitwise logic. A simple string like "YNYNNYYN" might be more immediately obvious.
Extensibility Adding a new allergy is simple, as long as you haven't exhausted the available bits. You just assign the next power of two. If you run out of bits (e.g., need a 65th allergy on a 64-bit integer), the entire system needs to be redesigned.
Database Querying Can be indexed and queried efficiently in most databases that support bitwise operators. Querying for "all patients with a peanut allergy" can be more complex than a simple `WHERE IsAllergicToPeanuts = true`.

Frequently Asked Questions (FAQ)

1. What exactly is a bitmask?
A bitmask is an integer used to manipulate specific bits within another integer. In our case, each allergen value (1, 2, 4, 8) is a bitmask designed to isolate and check a single bit in the allergy score.

2. Why are the allergy values required to be powers of two?
Using powers of two (1, 2, 4, 8, ...) ensures that each value corresponds to a single, unique bit in a binary number. This prevents ambiguity. If peanuts were 2 and shellfish were 3, a score of 5 could mean (2+3) or (1+4), making it impossible to decode reliably.

3. How does the FUNCTION BIT-AND work in Cobol?
BIT-AND is an intrinsic function introduced in later Cobol standards. It takes two numeric arguments and returns a new number that is the result of a bitwise AND operation between them. It's the cornerstone of this solution, allowing for the efficient checking of bits.

4. Can I add more allergies to this system?
Yes, absolutely. As long as you have available bits in your numeric field. The next allergy would be assigned the value 256 (28), then 512 (29), and so on. You would simply expand the ALLERGY-TABLE in the DATA DIVISION and update the loop boundary.

5. Is Cobol still relevant for new projects?
While rarely chosen for brand-new, greenfield projects, Cobol remains the backbone of the global financial and administrative world. Billions of lines of Cobol code run on mainframes today. Understanding it is crucial for maintaining and modernizing these critical systems. For more on the language, check out our complete Cobol guide.

6. What is the difference between PIC ... COMP and PIC ... DISPLAY?
DISPLAY is the default usage, storing numbers as a series of characters (e.g., the number 123 is stored as the characters '1', '2', '3'). COMP (computational) stores numbers in a binary format native to the machine's architecture. COMP is far more efficient for arithmetic and bitwise operations, making it the correct choice for our allergy score variable.

7. How should I handle an allergy score that is higher than the sum of all known allergens?
In our current program, any bits that don't correspond to a known allergen are simply ignored. For a more robust system, you could calculate the sum of all known allergen values (255 in our case). You can then use the BIT-AND function with the inverse of this sum to detect any "unknown" allergy bits and flag them for review.

Conclusion: Timeless Logic in a Classic Language

Decoding an allergy score in Cobol is more than just a programming exercise; it's a practical lesson in data efficiency and low-level manipulation. By mastering the use of bitwise operations like BIT-AND, you gain a powerful tool that transcends any single programming language. The principles of bitmasking are as relevant in modern Python or Go as they were in the heyday of the mainframe.

This challenge from the kodikra.com curriculum demonstrates that Cobol is not just about legacy code; it's about understanding the foundational, performance-critical techniques that underpin all of modern computing. The clean structure, readable logic, and raw efficiency of this solution are a testament to the enduring power of good software design.

Disclaimer: The solution provided is written for and tested with GnuCOBOL 3.1+. While intrinsic functions like BIT-AND are part of the modern Cobol standard, syntax and availability may differ on other compilers, such as IBM Enterprise COBOL for z/OS.


Published by Kodikra — Your trusted Cobol learning resource.