Bitwise Operators

keysymbols: ~, &, |, ^, (~^, ^~).

The bitwise operators are negation, and, or, xor and xnor. Bitwise operators perform a bit-by-corresponding-bit operation on both operands, if one operand is shorter it is bit extended to the left with zeros. See examples below.

        module bitTest;
           reg [3:0] a, b ,c;

           initial begin
              a = 4'b1100; b = 4'b0011; c = 4'b0101;

              $displayb(~a);     // bitwise negation, evaluates to 4'b0011
              $displayb(a & c);  // bitwise and, evaluates to 4'b0100
              $displayb(a | b);  // bitwise or, evaluates to 4'b1111
              $displayb(b ^ c);  // bitwise xor, evaluates to 4'b0110
              $displayb(a ~^ c); // bitwise xnor, evaluates to 4'b0110

           end
        endmodule // bitTest

EXERCISE
What do the following display?

              $displayb(~2'b01);
              $displayb(4'b1010 ^ 4'b10);
              $displayb(4'bx | 1'b1);

Answers


previous next contents