Assign With Operators
Part of the Fundamentals section of Coddy's Verilog journey. Lesson 38 of 90.
Once you understand continuous assignment, you can combine it with operators to create useful logic. The assign statement can use any operator to drive a wire.
Basic Syntax
assign wire_name = expression;The expression can include:
- Arithmetic operators (
+,-,*,/) - Bitwise operators (
&,|,^,~) - Logical operators (
&&,||,!)
- Comparison operators (
>,<,==,!=) - Shift operators (
<<,>>) - Conditional operator (
? :)
Examples with Different Operators
Bitwise AND:
assign out = a & b;Addition:
assign sum = a + b;Comparison:
assign is_greater = (a > b);Conditional (multiplexer):
assign out = sel ? a : b;Shift:
assign shifted = data << 2;Concatenation:
assign bus = {high_byte, low_byte};Code Example
module assign_operators (
input [3:0] a, b,
input sel,
output [3:0] and_out,
output [4:0] sum_out,
output is_equal,
output mux_out
);
assign and_out = a & b; // Bitwise AND
assign sum_out = a + b; // Addition
assign is_equal = (a == b); // Comparison
assign mux_out = sel ? a : b; // Conditional (multiplexer)
endmoduleMultiple Operators in One Assignment
You can combine operators in a single expression:
assign result = (a & b) | (c ^ d);
assign final = (a + b) > (c - d);
assign parity = ^data; // Reduction XOR (odd number of 1's)Operator Precedence
Verilog follows standard operator precedence. Use parentheses ( ) to make your intention clear:
// Unclear
assign out = a & b | c;
// Clear
assign out = (a & b) | c;Challenge
Add the missing assign statements based on the tasks.
What to do:
- Make
and_resultequal toinput_a AND input_b(bitwise) - Make
or_resultequal toinput_a OR input_b(bitwise) - Make
xor_resultequal toinput_a XOR input_b(bitwise) - Make
not_resultequal toNOT input_a(bitwise)
Try it yourself
module assign_challenge (
input input_a,
input input_b,
output and_result,
output or_result,
output xor_result,
output not_result
);
// TODO: Add assign statements for:
// and_result = input_a & input_b
// or_result = input_a | input_b
// xor_result = input_a ^ input_b
// not_result = ~input_a
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine LogicPractice on your own: Online Verilog compiler