# How to test all possible input for a logic function?

I have a logic expression: f0[a0_, a1_, a2_, a3_] := a0 And Not a1 And Not a2 And a3 Or Not a0 And a2 And a3 Or Not a0 And a1 And a3, I know I should use BooleanTable, but it cannot generate a table like below.

How to generate a truth table in mathematica like below?

• There is the BooleanTable command. Does it help? For creating the corresponding function you can use logical and: && and logical or: || in case you don't know. – user59583 Dec 3 '18 at 6:17
• @Buddha_the_Scientist Thank you, I have correct my expression, Of course BooleanTable can help me, but it cannot generate a table like the picture above, it only returns a series of true or false. – 余星佑 Dec 3 '18 at 6:23
• Then use the TableForm with the option TableHeadings for the result. Something like: TableForm[{{a, b, c, d}}, TableHeadings -> {None, {"a0", "a1", "a2", "a3"}}] – user59583 Dec 3 '18 at 6:24
• @Buddha_the_Scientist, thank you, but how to use my function to generate all series of possible outputs like this? I know that how to generate one possible output commad: TableForm[{{True, False, True, False, f0[True, False, True, False]}}, TableHeadings -> {None, {"a0", "a1", "a2", "a3", "b0"}}] – 余星佑 Dec 3 '18 at 6:44

f = a0 && ! a1 && ! a2 && a3

TableForm[BooleanTable[{a0, a1, a2, a3, f}, {a0, a1, a2, a3}], TableHeadings -> {None, {a0, a1, a2, a3, f}}]

Apparently there are resources like this one or this one

• Thank you very much! This absolutely solve my quesiton! – 余星佑 Dec 3 '18 at 7:20
truthTableFormattor[rawData_] := Insert[Insert[
Grid[rawData /. {0 -> 0,
1 -> Item[1, Background -> Lighter[Magenta]]},
FrameStyle -> Gray,
Frame -> All], {Background -> {None, {GrayLevel[0.7], {White}}},
Dividers -> {Black, {2 -> Black}}, Frame -> True,
Spacings -> {2, {2, {0.7}, 2}}}, 2], {Dividers -> All,
Spacings -> .7 {1, 1}}, 2];

truthTable[f__] :=  Module[{}, atoms = Cases[Most[{f}],
(a_ /;Length[a] == 0 \[And] Not[StringQ[a]])];heads =
rawData = Transpose@Boole[BooleanTable[#, atoms] & /@ {f}];
If[Last[{f}] === 1,
Transpose@Boole[BooleanTable[#, atoms] & /@ Most[{f}]],
If[Last[{f}] === "rev",
truthTableFormattor[{ToString[