sapdev logo background
sapdev logo sapdev logo
Comments

ABAP SET BIT Statement syntax, information and example SAP source code



Return to Statement index



SET BIT

Short Reference

ABAP Syntax_18 SET BIT bitpos OF byte_string [TO val].

What does it do? This statement sets the bit in bit position bitpos
of the variable byte_string to 1 or, if specified, to the value of data object val . The other bits in byte_string remain unchanged.
The data object byte_string must be byte-like, whereas
bitpos and val are
numerical expression positions of operand type i . The value of
bitpos must be greater than 0 and val must be either 0 or 1. Otherwise an exception that cannot be handled is raised. The bit positions in byte_string are counted from the beginning of the data object. If the value of bitpos is greater than the number of bits in byte_string , no bit is replaced and sy-subrc is set to 4.

System Fields
sy-subrc Meaning
0A bit in data object byte_string was set at position
bitpos .
4The value of bitpos is greater than the number of bits in byte_string , therefore no bit was set.

Latest notes: If byte_string has the deep
type xstring , a bit is set in the referenced byte string, not in the reference.
Character-like data objects are also allowed for byte_string in
non-Unicode programs .
Setting single bits is especially suited to preparing operators in bit expressions .
The predefined function bit-set can also be used to set an individual bit.

Example ABAP Coding In the data object hex with eight bits, the bit is set to the value 1 at the position determined by the loop counter sy-index , with all other bits set to 0. The output is "80 40 20 10 08 04 02 01".
DATA hex TYPE x LENGTH 1.
DATA output TYPE string.
DO 8 TIMES.
CLEAR hex.
SET BIT sy-index OF hex.
output = output <(> <)><(> <)> |{ hex } |.
ENDDO.
cl_demo_output=>display( output ).



Runtime Exceptions
Non-catchable Exceptions

Reason for error: The bit position is greater than or equal to 0 .
Runtime error: BIT_OFFSET_NOT_POSITIVE
Reason for error: The value of val is not equal to 0 or
1 .
Runtime error: BIT_NO_ZERO_OR_ONE
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved




SET
SET_BLANK_LINES




comments powered by Disqus