# XOR in Verilog?

in Verilog

What is a xor and how do you code a xor in Verilog?

by (220 points)

An 'exclusive or' that is a logical operation that outputs true or 1 only when inputs differ: 0 xor 1 = 1, 1 xor 1 = 0, true xor false = true, etc

This function is defined as the ^ operator in Verilog and is defined both for scalar and vector data types and may be a binary operator as well as an unary operator.

a 'xor' b is coded as a ^ b in Verilog.

by (240 points)

XOR gate is an exclusive-OR gate. The output is high only when one and only one of the inputs is high. If both inputs are high or if both inputs are low then the output is also low. There are several methods to code an XOR. Behavourial modelling is shown below:

``module xor_bm(c,a,b);input a,b;output c;reg c;always@(a,b)beginif (a==0 & b==0)  c=0;else if (a==1 & b==1)  c=0;else  c=1;endendmodule``
As mentioned earlier, when both inputs are the same (either both are high or both are low) then the output is low, otherwise it is high.

Another way to code an XOR is use the inbuilt verilog operator:
``module xor_ex2(c,a,b); input a,b; output c; assign c=(a^b); endmodule``