Unary Operators

Unary Bitwise Operations

A unary operation acts on a single vector. Suppose we have a bit-vector a, then we can apply several unary operations:

Operation Result Description
~a vector flip all bits of a
&a single bit AND all bits of a (1 if “all” a[i]==1)
|a single bit OR all bits of a (1 if “any” a[i]==1)
^a single bit XOR all bits of a (1 if “odd parity” in a)

The file src/testbench.v runs some examples of these operations. Run make to simulate the cases. The first line looks like this:

clk:            0   a:  00000000    ~a: 11111111    &a: 0   |a: 0   ^a: 0

Initially, a is all zeros, so &a = AND(0,0,0,0,0,0,0,0) = 0. The same applies to | and ^. The second line looks like this:

clk:            1   a:  00100100    ~a: 11011011    &a: 0   |a: 1   ^a: 0

Here we see a contains some zeros, so &a == 0. It also contains some ones, so |a == 1. Lastly, there are two non-zero bits in a, so it has even parity, therefore ^a == 0.

Now let’s examine another line:

clk:            5   a:  00001101    ~a: 11110010    &a: 0   |a: 1   ^a: 1

In this line, there are three non-zero bits in a, so it has odd parity, therefore ^a == 1.

Assigned Tasks

This is a simulation exercise (no build). In testbench.v, make these changes in both the $write and $fwrite lines (also edit the text descriptions to match what your code is doing):

Run make to simulate your changes, and check to make sure the correct output appears in test_result.txt.

When finished, turn in your work using git:

git add test_result.txt
git commit . -m "Complete"
git push origin main