Verilog Operators Part-I
Verilog Operators Part-I
VerilogOperatorsPartI
VerilogOperators
PartI
Feb92014
ArithmeticOperators
Binary:+,,*,/,%(themodulusoperator)
Unary:+,(Thisisusedtospecifythesign)
Integerdivisiontruncatesanyfractionalpart
Theresultofamodulusoperationtakesthesignofthefirstoperand
Ifanyoperandbitvalueistheunknownvaluex,thentheentireresult
valueisx
Register data types are used as unsigned values (Negative numbers
arestoredintwo'scomplementform)
Example
1modulearithmetic_operators();
2
3initialbegin
4$display("5+10=%d",5+10);
5$display("510=%d",510);
6$display("105=%d",105);
7$display("10*5=%d",10*5);
8$display("10/5=%d",10/5);
9$display("10/5=%d",10/5);
10$display("10%s3=%d","%",10%3);
11$display("+5=%d",+5);
12$display("5=%d",5);
13#10$finish;
14end
15
16endmodule
Youcoulddownloadfilearithmetic_operators.vhere
5+10=15
510=5
105=5
10*5=50
10/5=2
10/5=2
10%3=1
+5=5
5=5
RelationalOperators
Operator
a<b
a>b
a<=b
a>=b
Description
alessthanb
agreaterthanb
alessthanorequaltob
agreaterthanorequaltob
Theresultisascalarvalue(examplea<b)
0iftherelationisfalse(aisbiggerthenb)
http://www.asicworld.com/verilog/operators1.html
1/5
5/16/2015
VerilogOperatorsPartI
1iftherelationistrue(aissmallerthenb)
xifanyoftheoperandshasunknownxbits(ifaorbcontainsX)
Note:Ifanyoperandisxorz,thentheresultofthattestistreatedasfalse
(0)
Example
1modulerelational_operators();
2
3initialbegin
4$display("5<=10=%b",(5<=10));
5$display("5>=10=%b",(5>=10));
6$display("1'bx<=10=%b",(1'bx<=10));
7$display("1'bz<=10=%b",(1'bz<=10));
8#10$finish;
9end
10
11endmodule
Youcoulddownloadfilerelational_operators.vhere
5<=10=1
5>=10=0
1'bx<=10=x
1'bz<=10=x
EqualityOperators
There are two types of Equality operators. Case Equality and Logical
Equality.
Operator
a===b
a!==b
a==b
a!=b
Description
aequaltob,includingxandz(Caseequality)
anotequaltob,includingxandz(Caseinequality)
aequaltob,resultmaybeunknown(logicalequality)
a not equal to b, result may be unknown (logical
equality)
Operandsarecomparedbitbybit,withzerofillingifthetwooperands
donothavethesamelength
Resultis0(false)or1(true)
Forthe==and!=operators,theresultisx,ifeitheroperandcontains
anxoraz
For the === and !== operators, bits with x and z are included in the
comparisonandmustmatchfortheresulttobetrue
Note:Theresultisalways0or1.
Example
1moduleequality_operators();
2
3initialbegin
4//CaseEquality
5$display("4'bx001===4'bx001=%b",(4'bx001===4'bx001));
6$display("4'bx0x1===4'bx001=%b",(4'bx0x1===4'bx001));
7$display("4'bz0x1===4'bz0x1=%b",(4'bz0x1===4'bz0x1));
8$display("4'bz0x1===4'bz001=%b",(4'bz0x1===4'bz001));
9//CaseInequality
10$display("4'bx0x1!==4'bx001=%b",(4'bx0x1!==4'bx001));
11$display("4'bz0x1!==4'bz001=%b",(4'bz0x1!==4'bz001));
http://www.asicworld.com/verilog/operators1.html
2/5
5/16/2015
VerilogOperatorsPartI
12//LogicalEquality
13$display("5==10=%b",(5==10));
14$display("5==5=%b",(5==5));
15//LogicalInequality
16$display("5!=5=%b",(5!=5));
17$display("5!=6=%b",(5!=6));
18#10$finish;
19end
20
21endmodule
Youcoulddownloadfileequality_operators.vhere
4'bx001===4'bx001=1
4'bx0x1===4'bx001=0
4'bz0x1===4'bz0x1=1
4'bz0x1===4'bz001=0
4'bx0x1!==4'bx001=1
4'bz0x1!==4'bz001=1
5==10=0
5==5=1
5!=5=0
5!=6=1
LogicalOperators
Operator
!
&&
||
Description
logicnegation
logicaland
logicalor
Expressionsconnectedby&&and||areevaluatedfromlefttoright
Evaluationstopsassoonastheresultisknown
Theresultisascalarvalue:
0iftherelationisfalse
1iftherelationistrue
xifanyoftheoperandshasx(unknown)bits
Example
1modulelogical_operators();
2
3initialbegin
4//LogicalAND
5$display("1'b1&&1'b1=%b",(1'b1&&1'b1));
6$display("1'b1&&1'b0=%b",(1'b1&&1'b0));
7$display("1'b1&&1'bx=%b",(1'b1&&1'bx));
8//LogicalOR
9$display("1'b1||1'b0=%b",(1'b1||1'b0));
10$display("1'b0||1'b0=%b",(1'b0||1'b0));
11$display("1'b0||1'bx=%b",(1'b0||1'bx));
12//LogicalNegation
13$display("!1'b1=%b",(!1'b1));
14$display("!1'b0=%b",(!1'b0));
15#10$finish;
16end
17
18endmodule
Youcoulddownloadfilelogical_operators.vhere
1'b1&&1'b1=1
1'b1&&1'b0=0
1'b1&&1'bx=x
1'b1||1'b0=1
1'b0||1'b0=0
1'b0||1'bx=x
http://www.asicworld.com/verilog/operators1.html
3/5
5/16/2015
VerilogOperatorsPartI
!1'b1=0
!1'b0=1
BitwiseOperators
Bitwise operators perform a bit wise operation on two operands. They take
eachbitinoneoperandandperformtheoperationwiththecorrespondingbit
in the other operand. If one operand is shorter than the other, it will be
extended on the left side with zeroes to match the length of the longer
operand.
Operator
~
&
|
^
^~or~^
Description
negation
and
inclusiveor
exclusiveor
exclusivenor(equivalence)
Computationsincludeunknownbits,inthefollowingway:
~x=x
0&x=0
1&x=x&x=x
1|x=1
0|x=x|x=x
0^x=1^x=x^x=x
0^~x=1^~x=x^~x=x
Whenoperandsareofunequalbitlength,theshorteroperandiszero
filledinthemostsignificantbitpositions.
Example
1modulebitwise_operators();
2
3initialbegin
4//BitWiseNegation
5$display("~4'b0001=%b",(~4'b0001));
6$display("~4'bx001=%b",(~4'bx001));
7$display("~4'bz001=%b",(~4'bz001));
8//BitWiseAND
9$display("4'b0001&4'b1001=%b",(4'b0001&4'b1001));
10$display("4'b1001&4'bx001=%b",(4'b1001&4'bx001));
11$display("4'b1001&4'bz001=%b",(4'b1001&4'bz001));
12//BitWiseOR
13$display("4'b0001|4'b1001=%b",(4'b0001|4'b1001));
14$display("4'b0001|4'bx001=%b",(4'b0001|4'bx001));
15$display("4'b0001|4'bz001=%b",(4'b0001|4'bz001));
16//BitWiseXOR
17$display("4'b0001^4'b1001=%b",(4'b0001^4'b1001));
18$display("4'b0001^4'bx001=%b",(4'b0001^4'bx001));
19$display("4'b0001^4'bz001=%b",(4'b0001^4'bz001));
20//BitWiseXNOR
21$display("4'b0001~^4'b1001=%b",(4'b0001~^4'b1001));
22$display("4'b0001~^4'bx001=%b",(4'b0001~^4'bx001));
23$display("4'b0001~^4'bz001=%b",(4'b0001~^4'bz001));
24#10$finish;
25end
26
27endmodule
Youcoulddownloadfilebitwise_operators.vhere
~4'b0001=1110
~4'bx001=x110
~4'bz001=x110
4'b0001&4'b1001=0001
http://www.asicworld.com/verilog/operators1.html
4/5
5/16/2015
VerilogOperatorsPartI
4'b1001&4'bx001=x001
4'b1001&4'bz001=x001
4'b0001|4'b1001=1001
4'b0001|4'bx001=x001
4'b0001|4'bz001=x001
4'b0001^4'b1001=1000
4'b0001^4'bx001=x000
4'b0001^4'bz001=x000
4'b0001~^4'b1001=0111
4'b0001~^4'bx001=x111
4'b0001~^4'bz001=x111
Copyright19982014
DeepakKumarTalaAllrightsreserved
DoyouhaveanyComment?mailmeat:[email protected]
http://www.asicworld.com/verilog/operators1.html
5/5