Search code examples
csymbolsboolean-logicmux

What is the symbol of the `mux` chip in C?


I was studying logic gates when I came to know that each logic gate was already defined in C. For example, for the AND logic gate, the symbol is &. For OR, it is |. But I could not find a symbol for the MUX chip.

So, if there is a symbol for MUX, could someone tell me? If there isn't, could someone tell me how to replicate a MUX chip in C?


Solution

  • Please be advised that C operates at a much higher level of abstraction than logical gates, so making such comparisons might lead to confusions. That said, the closest you might come to a demultiplexer (I'll start with that since it's simpler) is the left shift operator:

    a << b
    

    This expression, assuming that a and b are int expressions, will produce a new int whose bits are the bits of a shifted to the left b times. For example, if a is 0100011011010110 and b is 3, the result will be 0011011010110000. Now, if a is either 0 or 1 and you interpret the resulting integer as being a bus, this corresponds to a demultiplexer.

    A multiplexer/selector can be implemented by the right shift operator >>, which shifts bits to the right. However, the result must be &'ed with 1 in order to clear any other bits than the one you were interested in:

    (c >> b) & 1
    

    This effectively selects the bit at index b (starting at the least significant bit) from c.