VLSIChaps Academy · Track 01

Operators, and the Logical Versus Bitwise Trap

~12 min Hands on lab 1 exercise Verilator
Page 06 of 20
01

The Families of Operators

SystemVerilog gives you several families of operators, and the first skill is knowing which family you are reaching for. Arithmetic operators do math like addition and multiplication. Relational operators compare two values and return a true or false answer. Logical operators reason about whole values being true or false. Bitwise operators work on each individual bit. Reduction operators collapse an entire vector down to a single bit.

Most operator bugs come from grabbing the right idea from the wrong family.

02

Logical Versus Bitwise: The Classic Trap

This is the mistake that catches almost every beginner, so slow down here. A logical operator asks a true or false question about whole values: is this value nonzero and that value nonzero. A bitwise operator works bit by bit and hands you back a full pattern. They look almost the same (the logical form simply doubles the symbol) but they compute completely different things.

Two values that are each nonzero can produce a true result with the logical operator and a result of zero with the bitwise one. Same inputs, opposite answers. That is exactly the trap this lab will spring on you, on purpose.

03

Reduction Operators

A reduction operator takes a single vector and applies an operation across all of its bits to produce one bit. With it you can ask, in a compact way, whether any bit in a vector is set, whether every bit is set, or the parity of the bits. It is a small but powerful tool for asking a question about an entire vector at once, instead of testing bits one by one.

04

Key Insight: Precedence and Equality

Two more traps complete the picture. First, precedence: operators do not all bind equally tightly, so an expression can group itself in a way you never intended. The fix is simple and free: add parentheses to state exactly how things group, and never trust your memory of the precedence table.

Second, equality: ordinary equality can return an unknown answer when unknown bits are involved, whereas case equality compares bit for bit and can detect an unknown exactly. When unknown values are in play, the equality operator you choose changes the answer you get.

Lab: Choose Logical or Bitwise, Correctly

Complete each expression with the correct operator. One expression needs a true or false answer about whole values; the other needs a bit by bit result. Run and submit. Swap one for the other and you will see the diverging value the engine computes.

Ask what each result should be. A single true or false answer about whole values wants the logical operator, the doubled symbol. A full bit pattern showing which bits are set in both wants the bitwise operator, the single symbol. They are not interchangeable.
Compiling and simulating...
Correct. You used the logical operator for the true or false question and the bitwise operator for the bit pattern. Same symbol family, completely different jobs, and you matched each one to its purpose.
Not quite. Your code compiled and ran, but a value is wrong. You used a bitwise operator where a true or false answer was needed, or a logical operator where a bit pattern was needed. The two are not interchangeable, and the engine computed a different number than you intended. The mentor below shows exactly where.
Compile error.
Something went wrong reaching the engine. Try again in a moment.
AI Mentor
Thinking...
📲 Join 20K+ Engineers