 ## Setting and Reading Bits In a hexadecimal field (type x), you can set or read individual bits.

### Setting Bits

To set an individual bit, use the statement

SET BIT n OF f [TO b].

This statement sets the bit at position n of the field f to 1, or to the value of field b. The system must be able to interpret bit n as a positive whole number. The field f must have data type x. The field b must contain the value 0 or 1. If the bit is set, sy-subrc is set to 0. If n is greater than the length of f, sy-subrc is not equal to zero. If n or b contain invalid values, a runtime error occurs. DATA hex(3) TYPE x.

SET BIT: 09 OF hex TO 1,
10 OF hex TO 0,
11 OF hex TO 1,
12 OF hex TO 1,
13 OF hex TO 0,
14 OF hex TO 1,
15 OF hex TO 0,
16 OF hex TO 1.

WRITE hex.

The bits of the second byte in the three-character hexadecimal field hex are set to ‘10110101’, and the list output is as follows:

00B500

The decimal value of the second byte is 181.

To read an individual bit, use the statement

GET BIT n OF f INTO b.

This statement reads the bit at position n of the field f into field b .The system must be able to interpret bit n as a positive whole number. The field f must have data type x. If the bit is read, sy-subrc is set to 0. If n is greater than the length of f, sy-subrc is not equal to zero and b is set to zero. If n contains an invalid value, a runtime error occurs. DATA: hex(1) TYPE x VALUE 'B5',
b(1) TYPE n.

DO 8 TIMES.
GET BIT sy-index OF hex INTO b.
WRITE b NO-GAP.
ENDDO.

Here, the eight buts of the single-character hexadecimal field hex (value ‘B5’) are read and displayed as follows:

10110101 