Multplexer 4x1 program

I am utterly lost, I am making a program based on a 4x1 multiplexer and output the truth table. But I am not sure how to go about it,this is the multiplexer https://imgur.com/FS9kco2, from what I understand, a and b have to be certain combination to get A-D and send those inputs to Q. So I think I have to do a nand on a,b get that result and do a nand on result,b and so on. And based on those results I allow A-D to get send to Q? But this is all I have so far, not sure where to go from here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  bool A, B, C, D, SELa, SELb;
    bool result1,result2,result3,result4;

    
    //return 1 for a 2 for b 3 for c 4 for d
    cout << "Multiplexer" << endl;
    cout << "--------------" << endl;
    
    SELa = false;
    SELb = false;
    
    result1 = (!(SELa && SELb));
    
    result2 = (!(result1 && SELb));

    result3 = (!(result2 && SELb));
    
    result4 = (!(result3 && SELb));
> bool A, B, C, D, SELa, SELb;
Ok so far

> bool result1,result2,result3,result4;
Try just
bool Q;

> //return 1 for a 2 for b 3 for c 4 for d
Why are you returning integers?
If you just need a number, you only need SELa and SELb.

1
2
3
4
5
6
7
8
bool multiplex(bool A, bool B, bool C, bool D, bool sela, bool selb) {
    bool Q;

    // now use sela and selb to decide which of A to D should
    // be assigned to Q.

    return Q;
}

A-D to get send to Q?

Looks like A, B, C, D, and Q have same type, but do we actually care what it is?
1
2
3
4
5
6
7
template <typename U>
U multiplex(U A, U B, U C, U D, bool sela, bool selb) {
    U Q;
    // now use sela and selb to decide which of A to D should
    // be assigned to Q.
    return Q;
}

There is no need for the Q in that function either, since each case can return approriate value.
As a C++20 starter, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <concepts>
#include <iostream>

template<typename... Args>
requires (sizeof ...(Args) > 1) && (std::convertible_to<Args, bool> && ...)
bool And(Args... args)
{
	return (... && args);
}

template<typename... Args>
bool Nand(Args... args) {
	return !And(args...);
}

bool evaluate(bool selb, bool sela, bool D, bool C, bool B, bool A) {
	const auto notb { Nand(selb, selb) };
	const auto nota { Nand(sela, sela) };

	return Nand(Nand(A, notb, nota), Nand(B, notb, sela), Nand(C, nota, selb), Nand(D, selb, sela));
}

int main() {
	std::cout << "b a D C B A   Q\n";

	for (bool b : {0, 1})
		for (bool a : {0, 1})
			for (bool D : {0, 1})
				for (bool C : {0, 1})
					for (bool B : {0, 1})
						for (bool A : {0, 1})
							if (auto Q { evaluate(b, a, D, C, B, A) }; Q)
								std::cout << b << ' ' << a << ' ' << D << ' ' << C << ' ' << B << ' ' << A << "   " << Q << '\n';
}


which shows the truth table for all true Q:


b a D C B A   Q
0 0 0 0 0 1   1
0 0 0 0 1 1   1
0 0 0 1 0 1   1
0 0 0 1 1 1   1
0 0 1 0 0 1   1
0 0 1 0 1 1   1
0 0 1 1 0 1   1
0 0 1 1 1 1   1
0 1 0 0 1 0   1
0 1 0 0 1 1   1
0 1 0 1 1 0   1
0 1 0 1 1 1   1
0 1 1 0 1 0   1
0 1 1 0 1 1   1
0 1 1 1 1 0   1
0 1 1 1 1 1   1
1 0 0 1 0 0   1
1 0 0 1 0 1   1
1 0 0 1 1 0   1
1 0 0 1 1 1   1
1 0 1 1 0 0   1
1 0 1 1 0 1   1
1 0 1 1 1 0   1
1 0 1 1 1 1   1
1 1 1 0 0 0   1
1 1 1 0 0 1   1
1 1 1 0 1 0   1
1 1 1 0 1 1   1
1 1 1 1 0 0   1
1 1 1 1 0 1   1
1 1 1 1 1 0   1
1 1 1 1 1 1   1

a very beginner friendly take on it:
1
2
3
4
5
6
7
8
9
10
11
int mux(int a, int b, int c, int d, bool sel1, bool sel2)
{
   unsigned char sel2num = sel2*2+sel1; //converts to int of binary 00, 01, 10, 11 
   switch sel2num
    { 
      case 0: return a;
      case 1: return b;
      case 2: return c;
      case 3: return d;
    }
}


your case to sel mapping may be some other way, but all you have to do is figure out which goes where and order them as you need.
Last edited on
Topic archived. No new replies allowed.