What is the symbol of the `mux` chip in C?
Asked Answered
B

4

6

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?

Bysshe answered 17/1, 2016 at 5:34 Comment(2)
How exactly would you expect a MUX operator to work? How would you supply its inputs?Helminthology
@Dmitri, I would make a function in which I will pass it's inputs.Bysshe
H
4

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.

Hardie answered 17/1, 2016 at 5:44 Comment(11)
So you mean to say, that you cannot make a MUX in C? (By the way I didn't downvote or upvote anyone's answer till now.).Bysshe
Looks like we all got one except for one answer that immediately got upvoted. I'll go ahead and cancel out the downvote you got.Elgar
do you even know what a mux is?Item
@AshishAhuja: What I'm saying is that the question doesn't make sense because a MUX is a digital circuit, not a programming construct. However, if you interpret int values as buses, you can make something that has similar effect - but when your program is executed, the operation will most likely not be executed by an actual MUX (except in the sense that MUXes are a part of the Arithmetic-Logic Unit of your processor, but this is, as mentioned, on a much lower level).Hardie
@Adam: Thanks; same to you.Hardie
@AasmundEldhuset, yeah I wanted to make a virtual MUX which will not be executed by an actual MUX. Just virtual.Bysshe
@AshishAhuja: Then, I believe that my suggested expressions will do what you want (as long as your bus doesn't have more bits than an int, typically 32).Hardie
@Jasen, yes I know what a MUX is. It is a chip which selects one of several analog signals (or maybe digital), and forwards the input into a single line.Bysshe
...I, on the other hand, mixed up the definitions of multiplexer and demultiplexer; a correction is coming shortly.Hardie
mux is like CD4519, analogue switches are different.Item
One more downvote - can the downvoters please explain why?Hardie
I
6

Closest is the conditional operator: ? :

eg:

 x ? b : a 

if x is 0 you get a if it's 1 (or anything else) you get b

This operator works on entire values, like || && == and ! do. It does not operate on bits as ^ ~ & and | do.

There is no direct equivalent for a multi-input mux. but you can fake one using an anonymous array, eg:

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

but many people frown on constructions of that form.

if you need a bitwise mux you'll need to build it from the bitwise operators eg:

 a ^ (( b ^ a ) & x)
Item answered 17/1, 2016 at 5:47 Comment(0)
H
4

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.

Hardie answered 17/1, 2016 at 5:44 Comment(11)
So you mean to say, that you cannot make a MUX in C? (By the way I didn't downvote or upvote anyone's answer till now.).Bysshe
Looks like we all got one except for one answer that immediately got upvoted. I'll go ahead and cancel out the downvote you got.Elgar
do you even know what a mux is?Item
@AshishAhuja: What I'm saying is that the question doesn't make sense because a MUX is a digital circuit, not a programming construct. However, if you interpret int values as buses, you can make something that has similar effect - but when your program is executed, the operation will most likely not be executed by an actual MUX (except in the sense that MUXes are a part of the Arithmetic-Logic Unit of your processor, but this is, as mentioned, on a much lower level).Hardie
@Adam: Thanks; same to you.Hardie
@AasmundEldhuset, yeah I wanted to make a virtual MUX which will not be executed by an actual MUX. Just virtual.Bysshe
@AshishAhuja: Then, I believe that my suggested expressions will do what you want (as long as your bus doesn't have more bits than an int, typically 32).Hardie
@Jasen, yes I know what a MUX is. It is a chip which selects one of several analog signals (or maybe digital), and forwards the input into a single line.Bysshe
...I, on the other hand, mixed up the definitions of multiplexer and demultiplexer; a correction is coming shortly.Hardie
mux is like CD4519, analogue switches are different.Item
One more downvote - can the downvoters please explain why?Hardie
E
1

C has four bitwise operators:

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

There is no MUX operator.

Be careful about your phrasing. These are called bitwise operators, and are analogous to logic gates applied to all bits in an integral type. In C logical operators are different.

Elgar answered 17/1, 2016 at 5:43 Comment(4)
So is there a way to make a MUX chip using these?Bysshe
@AshishAhuja a MUX isn't something you'd normally code at the C level. The same task is usually accomplished in several other ways, such as a conditional (if, switch or ?:), and array lookup, bit packing, or others. It all depends on what you want to use your MUX for. Remember that C is not a hardware language, it's a high level language.Elgar
thanks for informing me that, but if you want to do it, what is wrong. I am doing this as I was learning circuits and hardware. And, I didn't want to do soldering, as the last I did soldering on a circuit, I burned one of my finger.Bysshe
soldering without burning fingers is an important skill. not a skill I have fully mastered, I count it a success if I have no blisters the next day.Item
D
1

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.

Daune answered 17/1, 2016 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.