← All exercises

Set Selected Bits

easy

Forces selected bits of a word on while preserving every other input bit.

What it teaches

Use an OR mask to add required flags to a packed command word without clearing flags already present. A 1 in the mask forces that bit on; a 0 leaves the matching input bit unchanged. Example: input 80 is 0101 0000 and mask 3 is 0000 0011, so the result is 0101 0011, or 83. The mirror trick — forcing selected bits off — is an AND with the inverted mask.

Inputs and outputs

TagTypeDefaultDescription
In_WordDINT80Packed word whose existing 1 bits are preserved
Cfg_SetMaskDINT3A 1 forces the matching output bit on
Out_WordDINTInput word with selected bits forced on

Required behavior

  • Each bit of Out_Word is 1 when the matching bit of In_Word or Cfg_SetMask is 1.
  • A 1 in Cfg_SetMask forces the matching output bit to 1; a 0 preserves the matching In_Word bit.
  • Cfg_SetMask can set an input bit but cannot clear one.
  • With the defaults, In_Word 80 (0101 0000) and Cfg_SetMask 3 (0000 0011) produce 83 (0101 0011).
  • All 32 bits are evaluated every scan.
  • A mask of 0 copies In_Word, while a mask of -1 sets all 32 output bits.