Basic logic gates

Map the truth tables for AND, OR, NOT, NAND, NOR and XOR to gates. Then use four-bit inputs to work out the output width of a bitwise operation, a logical test and a reduction.

The collector represents four sensor states in each word. a=1010 has ones on channels three and one, and b=1100 has ones on channels three and two. Finding every channel that is high in both words needs four output bits. Asking whether both words have at least one high channel needs one bit.

Decide whether to operate on each bit or on a whole word

Bitwise AND (a & b) pairs bits at matching positions. Only channel three is high on both sides, so the result is 1000. OR needs either side to be high and gives 1110. XOR requires a difference and gives 0110. NOT flips the bits of A to produce 0101. NAND flips the AND word to 0111. NOR flips the OR word to 0001.

A single-bit truth table gives the result for each position. NOT has one input and flips zero to one or one to zero.

ABANDORXORNANDNOR
0000011
0101110
1001110
1111000

Logical AND (a && b) treats both words as conditions. Each word is nonzero, so it returns the single bit 1. A reduction works on one input word. &a asks whether all four bits are high and gives 0. ^a returns one if the count of high bits is odd. 1010 has two ones, so its parity is 0.

A bit, a word, or one true/false result?. Bitwise, logical and reduction operators have different output widths
Bitwise, logical and reduction operators have different output widths

Synthesizable SystemVerilog

module logic_gates4 (
    input  logic [3:0] a,
    input  logic [3:0] b,
    output logic [3:0] and_bits,
    output logic [3:0] or_bits,
    output logic [3:0] xor_bits,
    output logic [3:0] not_a,
    output logic [3:0] nand_bits,
    output logic [3:0] nor_bits,
    output logic logic_and,
    output logic all_a,
    output logic parity_a
);
    assign and_bits = a & b;
    assign or_bits = a | b;
    assign xor_bits = a ^ b;
    assign not_a = ~a;
    assign nand_bits = ~(a & b);
    assign nor_bits = ~(a | b);
    assign logic_and = a && b;
    assign all_a = &a;
    assign parity_a = ^a;
endmodule

Check the results against the waveform

The inputs and outputs are shown in binary.

Change ataband_bitslogic_andall_aparity_a
0 ns101011001000100
10 ns101000000000000
20 ns111100000000010
30 ns011100110011101
When a = 1111, reduction AND is 1 and parity is 0
The plot uses the VCD generated by the Icarus run.

With a=1111 and b=0000, reduction AND gives 1 and logical AND remains 0. The first checks every bit of A, and the second checks both words as conditions. Choose the operator according to whether the receiver needs a mask or a flag.

Download the runnable SystemVerilog lab. Icarus checks the 4 rows above and 256 binary input combinations. It reads each directed result 1 ns after changing the inputs. The wait sets an observation time in the testbench. The waveform is functional evidence. Physical delay needs a timing model.

Interactive lab

Change the two four-bit inputs. Compare four-bit bitwise words with one-bit logical and reduction results.

Bitwise AND
Bitwise OR
Bitwise XOR
NOT A
NAND
NOR
Logical AND
Reduction AND of A
Parity of A
Show the last eight observations

    Teaching film

    Read the transcript

    Run the RTL yourself

    Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.

    Download Basic logic gates RTL (.zip)

    Check your understanding

    1. A=1010 and B=1100. What is the four-bit bitwise AND?
    2. For A=1010 and B=1100, how wide is A && B?
    3. For A=1111 and B=0000, which pair is &A and A && B?
    4. What is ^A if A=0111?
    5. Which operation marks the positions where A and B differ?