The |
, &
, and ~
operators are bitwise operators. They work in parallel on individual bits in the operands. There is no corresponding bitwise operator for a multiplexer. The ternary operator:
output = cond ? a : b
comes close, but the selector operand is treated as a single bit, and not a vector of bits (that is, all output bits come from a
or all output bits come from b
, you can't have some of the output bits come from a
and some come from b
). To get a true bitwise multiplexer, where the selector is a vector that selects individual bits from a
or b
, you can implement it the way you would build one from discrete logic gates:
output = (cond & a) | (~cond & b);
Here, a 1
in a cond
bit allows the corresponding bit from a
to pass, and blocks the corresponding bit from b
(because b
is masked with the inverse condition). A 0
in a cond
bit blocks the corresponding bit from a
, and allows the corresponding bit from b
to pass. The two masked values are bitwise OR'd together, so a bit of output is either the corresponding bit from a
of b
, depending upon the state of the corresponding bit in c
.