Replace Selected Bits
medium
Replaces selected bits in an existing word while preserving every unselected bit.
What it teaches
Unlike Apply Bit Mask, a blocked position does not become 0: it comes from a separate Base word. A 1 replaces that position with the Source bit; a 0 keeps the Base bit. Example: Base 240 is 1111 0000, Source 6 is 0000 0110, and Mask 15 is 0000 1111, so the result is 1111 0110, or 246. The same three inputs rebuild the result every scan.
Inputs and outputs
| Tag | Type | Default | Description |
|---|---|---|---|
| In_Source | DINT | 6 | Replacement bits used where the mask is 1 |
| In_Base | DINT | 240 | Existing word kept where the mask is 0 |
| Cfg_Mask | DINT | 15 | A 1 replaces from Source; a 0 keeps Base |
| Out_Word | DINT | — | Base word with selected bits replaced |
Required behavior
- For each bit, a 1 in Cfg_Mask copies the matching bit from In_Source to Out_Word.
- For each bit, a 0 in Cfg_Mask copies the matching bit from In_Base to Out_Word.
- Out_Word is rebuilt every scan; it does not retain an earlier value.
- With the defaults, Base 240 (1111 0000), Source 6 (0000 0110), and Mask 15 (0000 1111) produce 246 (1111 0110).
- A mask of 0 copies all of In_Base, and a mask of -1 copies all 32 bits of In_Source.