← 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