A bit field packs many on-off switches into one binary storage unit. Instead of ten one-character Y/N flags consuming ten bytes, a two-byte binary halfword holds sixteen independent bits—each bit can mean record updated, tax exempt, or error pending. Easytrieve represents bit containers as type B fields without decimal positions—non-quantitative mode where every bit is data, not sign. You set and test bits with AND, OR, and XOR assignments using hex mask literals from the hex values page. Mis-declare the same bytes as quantitative B 2 0 and the high bit becomes sign, corrupting flag semantics. Interface programs reading COBOL COMP status words or CICS response codes depend on correct bit handling. This page teaches bit numbering, mask tables, set-clear-test patterns, and when character flags beat bit packing for maintainability.
Define STATUS 20 2 B without decimal positions for sixteen flag bits starting at byte twenty of the record. Length one gives eight bits; length four gives thirty-two. Do not add decimal positions unless the bytes represent signed binary numbers—not individual flags. VALUE X'0000' initializes all bits off at program start for working storage.
12345FILE CONTROL FB(80 800) STATUS-WORD 10 2 B COUNT 12 4 B 0 DEFINE WS-FLAGS W 2 B VALUE X'0000'
STATUS-WORD is non-quantitative flags from file. COUNT is quantitative binary integer with zero decimals—high bit is sign, not flag bit zero. Never mix both semantics on the same field definition.
Convention in many mainframe docs numbers bits 0 through 15 left to right within the hex display of the field—bit 0 is often least significant bit of the rightmost byte. Verify your shop diagram—some documentation reverses bit order in diagrams. Consistency matters more than universal standard when writing masks.
| Bit | Set mask (OR) | Clear mask (AND) |
|---|---|---|
| 0 | X'0001' | X'FFFE' |
| 1 | X'0002' | X'FFFD' |
| 2 | X'0004' | X'FFFB' |
| 3 | X'0008' | X'FFF7' |
| 7 | X'0080' | X'FF7F' |
Set bit: field = field OR mask. Clear bit: field = field AND clear-mask where clear-mask has zero at target bit. Toggle: field = field XOR mask. Broadcom assignment examples demonstrate packed fields with MASK HEX; same operators apply to B containers. Always assign back to same field or another field of matching length.
12345678* Set bit 0 on WS-FLAGS = WS-FLAGS OR X'0001' * Clear bit 0 WS-FLAGS = WS-FLAGS AND X'FFFE' * Toggle all bits in two-byte field WS-FLAGS = WS-FLAGS XOR X'FFFF'
Copy field to work area, AND with test mask, compare result to zero or to mask. Some shops define bit-test PROCs. Simpler path for beginners: maintain parallel A 1 flags updated whenever bits change—trade storage for readable IF ACTIVE-FLAG EQ Y. High-volume interfaces keep bits only to match external layout.
12345DEFINE WS-TEST W 2 B WS-TEST = STATUS-WORD AND X'0001' IF WS-TEST NE 0 DISPLAY 'BIT 0 IS ON' END-IF
| Approach | Advantages | Disadvantages |
|---|---|---|
| B bit field | Compact; matches APIs | Harder to read in dumps without hex |
| A 1 Y/N | Readable IF and reports | One byte per flag |
| N 1 0 | Numeric 0/1 tests | Not standard for all interfaces |
COBOL copybooks document flag bytes as PIC S9(4) COMP or binary items with level-88 condition names per bit. Easytrieve maps the container as B 2 non-quantitative. Individual 88-level names become documentation for your mask constants—BIT-ACTIVE equals OR X'0004' when copybook says bit two means active. Transcribe mask values from copybook comments, not from guesswork.
OR X'000F' sets low four bits at once. AND X'FFF0' clears low four bits. Design mask constants as named VALUE fields in working storage for maintainability—avoid magic hex scattered through fifty assignment lines without comments.
FILE STATUS-WORD reads bits from input dataset. Do not assume zero on read—validate before testing bits on new file formats. FLDCHK debug helps catch length errors that misalign STATUS-WORD relative to COUNT quantitative field following it.
A bit field is a row of light switches in one small box. Each switch is on or off. Masks are instructions saying flip only the third switch or turn off the first switch. You cannot see switch labels in normal numbers—you need hex goggles (DISPLAY HEX) to see which lights are on.
1. Bit flags in Easytrieve typically use type B without:
2. To clear bit 0 of a two-byte field you might AND with:
3. Quantitative B with B 2 0 differs from non-quantitative B 2 because:
4. OR X'0001' on a flag halfword:
5. Best debug view for bit fields is: