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: in Binary, Base 240 ends in 1111_0000, Source 6 in 0000_0110 and Mask 15 in 0000_1111, so the result ends in 1111_0110, which is 246; every digit before those is 0. The same three inputs rebuild the result every scan.
Parameters
| Tag | Direction | Type | Default | Description |
|---|---|---|---|---|
| In_Source | Input | DINT | 6 | Replacement bits used where the mask is 1 |
| In_Base | Input | DINT | 240 | Existing word kept where the mask is 0 |
| Cfg_Mask | Input | DINT | 15 | A 1 replaces from Source; a 0 keeps Base |
| Out_Word | Output | DINT | 0 | 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, Source 6 and Mask 15 produce 246. In Binary their last eight digits are 1111_0000, 0000_0110, 0000_1111 and 1111_0110, and every digit before them is 0.
- A mask of 0 copies all of In_Base, and a mask of -1 copies all 32 bits of In_Source.