The BIT data type in COBOL represents the most memory-efficient way to store boolean values and flags, using individual bits or groups of bits to represent true/false conditions, status indicators, and configuration settings. This data type is essential for modern COBOL applications requiring optimal memory utilization, high-performance flag operations, embedded systems programming, and scenarios where thousands of boolean conditions must be stored and processed efficiently while maintaining compatibility with system-level programming requirements and database optimization strategies.
The BIT data type allows storage of boolean values using individual bits or bit strings. Each bit can represent a true (1) or false (0) condition, and multiple bits can be grouped together to create compact flag arrays or status registers for efficient memory usage and fast bitwise operations.
123456789101112131415161718192021DATA DIVISION. WORKING-STORAGE SECTION. 01 SYSTEM-FLAGS. 05 DEBUG-MODE BIT. 05 VERBOSE-OUTPUT BIT. 05 AUTO-SAVE BIT. 05 BACKUP-ENABLED BIT. 01 PERMISSION-FLAGS BIT(8). *> Individual bit positions for different permissions 01 STATUS-REGISTER. 05 PROCESSING-FLAGS BIT(16). 88 INIT-COMPLETE BIT(1) VALUE B'1'. 88 ERROR-OCCURRED BIT(1) VALUE B'1'. 88 BACKUP-NEEDED BIT(1) VALUE B'1'. PROCEDURE DIVISION. SET DEBUG-MODE TO TRUE. SET VERBOSE-OUTPUT TO FALSE. SET AUTO-SAVE TO TRUE.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657*> Advanced bit manipulation operations DATA DIVISION. WORKING-STORAGE SECTION. 01 BIT-OPERATIONS. 05 SOURCE-BITS BIT(8) VALUE B'10110100'. 05 MASK-BITS BIT(8) VALUE B'11110000'. 05 RESULT-BITS BIT(8). 05 SINGLE-BIT BIT. 01 CONFIGURATION-BITS BIT(32). 88 FEATURE-A BIT 1 VALUE B'1'. 88 FEATURE-B BIT 2 VALUE B'1'. 88 FEATURE-C BIT 3 VALUE B'1'. 88 DEBUG-ENABLED BIT 4 VALUE B'1'. 88 LOGGING-ENABLED BIT 5 VALUE B'1'. 01 SYSTEM-STATE. 05 ONLINE-STATUS BIT. 05 MAINTENANCE-MODE BIT. 05 BACKUP-RUNNING BIT. 05 ALERTS-ENABLED BIT. PROCEDURE DIVISION. PERFORM-BITWISE-OPERATIONS. *> AND operation AND SOURCE-BITS WITH MASK-BITS GIVING RESULT-BITS. DISPLAY "AND Result: " RESULT-BITS. *> OR operation OR SOURCE-BITS WITH MASK-BITS GIVING RESULT-BITS. DISPLAY "OR Result: " RESULT-BITS. *> XOR operation EXCLUSIVE-OR SOURCE-BITS WITH MASK-BITS GIVING RESULT-BITS. DISPLAY "XOR Result: " RESULT-BITS. SET-CONFIGURATION-FLAGS. SET FEATURE-A TO TRUE. SET DEBUG-ENABLED TO TRUE. SET LOGGING-ENABLED TO FALSE. IF FEATURE-A AND DEBUG-ENABLED DISPLAY "Debug mode enabled for Feature A" END-IF. CHECK-SYSTEM-STATUS. SET ONLINE-STATUS TO TRUE. SET MAINTENANCE-MODE TO FALSE. EVALUATE TRUE WHEN ONLINE-STATUS AND NOT MAINTENANCE-MODE DISPLAY "System is online and operational" WHEN MAINTENANCE-MODE DISPLAY "System is in maintenance mode" WHEN NOT ONLINE-STATUS DISPLAY "System is offline" END-EVALUATE.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960*> Large-scale flag management DATA DIVISION. WORKING-STORAGE SECTION. 01 USER-PERMISSIONS. 05 PERMISSION-ARRAY BIT(1000). *> Can store 1000 boolean permissions in 125 bytes 01 INVENTORY-FLAGS. 05 ITEM-STATUS OCCURS 10000 TIMES BIT. *> Status for 10,000 items in 1,250 bytes 01 DAILY-OPERATIONS. 05 HOURLY-FLAGS BIT(24). 88 HOUR-PROCESSED BIT OCCURS 24 TIMES VALUE B'1'. 01 FEATURE-MATRIX. 05 CUSTOMER-FEATURES OCCURS 1000 TIMES. 10 ENABLED-FEATURES BIT(64). 88 PREMIUM-ACCESS BIT 1 VALUE B'1'. 88 MOBILE-ACCESS BIT 2 VALUE B'1'. 88 API-ACCESS BIT 3 VALUE B'1'. 88 ANALYTICS BIT 4 VALUE B'1'. PROCEDURE DIVISION. INITIALIZE-USER-PERMISSIONS. PERFORM VARYING WS-USER-ID FROM 1 BY 1 UNTIL WS-USER-ID > 1000 IF WS-USER-LEVEL(WS-USER-ID) = "ADMIN" SET PERMISSION-ARRAY(WS-USER-ID) TO TRUE ELSE SET PERMISSION-ARRAY(WS-USER-ID) TO FALSE END-IF END-PERFORM. PROCESS-INVENTORY-STATUS. PERFORM VARYING WS-ITEM FROM 1 BY 1 UNTIL WS-ITEM > 10000 IF WS-QUANTITY(WS-ITEM) > 0 SET ITEM-STATUS(WS-ITEM) TO TRUE ELSE SET ITEM-STATUS(WS-ITEM) TO FALSE END-IF END-PERFORM. UPDATE-CUSTOMER-FEATURES. PERFORM VARYING WS-CUSTOMER FROM 1 BY 1 UNTIL WS-CUSTOMER > 1000 IF WS-SUBSCRIPTION-LEVEL(WS-CUSTOMER) = "PREMIUM" SET PREMIUM-ACCESS(WS-CUSTOMER) TO TRUE SET MOBILE-ACCESS(WS-CUSTOMER) TO TRUE SET API-ACCESS(WS-CUSTOMER) TO TRUE SET ANALYTICS(WS-CUSTOMER) TO TRUE ELSE SET PREMIUM-ACCESS(WS-CUSTOMER) TO FALSE SET API-ACCESS(WS-CUSTOMER) TO FALSE END-IF END-PERFORM.
Storage Method | 1000 Flags | Memory Usage | Efficiency |
---|---|---|---|
BIT array | 125 bytes | 125 bytes | 100% |
PIC X fields | 1000 bytes | 1000 bytes | 12.5% |
PIC 9 fields | 1000 bytes | 1000 bytes | 12.5% |
Use 88-level condition names to define specific bit positions, or use bit indexing with BIT(n) where n is the bit position. You can then use IF statements or SET statements to test and modify individual bits.
BIT data is primarily for boolean operations. For arithmetic, convert to numeric types first. However, you can perform bitwise logical operations like AND, OR, XOR, and NOT on BIT data types.
A BIT(n) field uses (n + 7) / 8 bytes, rounded up to the nearest byte. For example, BIT(8) uses 1 byte, BIT(16) uses 2 bytes, and BIT(25) uses 4 bytes (32 bits with 7 unused bits).