This question:
How do you set, clear, and toggle a single bit?
discusses three operations on a specific bit within a larger value, which correspond in a straightforward manner to OR 1, AND 0, and XOR 1 on that bit. But what if we don't know that second bit's value in advance? What if we want perform an assignment, where we don't know the other operand bit at compile-time? That is, we want set one bit, at a certain position a bit block, to the value of a new unrelated bit provided at run-time?
I'd like to have the fastest possible implementation for this on, say, Intel x86_64 (and ignoring vectorization, which I hope is irrelevant here). Also, suppose for simplicity the bit block type is uint32_t
.
Edit: Made my answer C rather than C++, since there's nothing C++'ish about the question really.