What is the `mux` character in C?

I studied logical gates when I found out that every logical gateway is already defined in C For example, for a logical logical AND element, the & symbol. For OR this is | . But I could not find the symbol for the MUX chip.

So, if there is a symbol for MUX , can someone tell me? If this does not happen, can someone tell me how to copy the MUX chip to C?

+5
source share
4 answers

Please keep in mind that C operates at a much higher level of abstraction than logic gates, so such comparisons can lead to confusion. However, the closest you can come to the demultiplexer (I will start from this, as it is easier) is the left shift operator:

 a << b 

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

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

 (c >> b) & 1 

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

+3
source

The closest is the conditional statement ? :

eg:

  x ? b : a 

if x is 0 , you get a , if it is 1 (or something else) you get b

This operator works with all values, such as || && == and ! do. It does not work with bits like ^ ~ & and | do.

There is no direct equivalent for multi-module multiplex. but you can fake one using an anonymous array, for example:

  ((int[]){a,b,c,d,})[x] 

but many people are unhappy with the designs of this form.

if you need a bitwise multiplexer, you will need to build it from bitwise operators for example:

  a ^ (( b ^ a ) & x) 
+6
source

C has four bitwise operators:

  • And, & , as in a & b
  • OR, | as in a | b a | b
  • XOR, ^ , as in a ^ b
  • NOT ~ , as in ~a

No MUX operator.

Be careful with your phrasing. They are called bitwise operators and are similar to logical lines applied to all bits of an integral type. In C, logical operators are different.

+1
source

Operators | , & and ~ are bitwise operators. They work in parallel on separate bits in the operands. There is no corresponding bitwise operator for the multiplexer. Ternary operator:

 output = cond ? a : b 

is close, but the selector operand is processed as a single bit, not a bit vector (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 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 could build it from a discrete logic gate:

 output = (cond & a) | (~cond & b); 

Here, bit 1 in cond allows you to transfer the corresponding bit from a and blocks the corresponding bit from b (because b masked by the inverse condition). A 0 in the cond bit blocks the corresponding bit from a and allows the corresponding bit from b to be transmitted. The two masked values ​​are bitwise OR'd together, so the output bit is either the corresponding bit from a b , depending on the state of the corresponding bit in c .

+1
source

Source: https://habr.com/ru/post/1240787/


All Articles