Logic to Invert bits of a Variable

  • This post describes  all about simple logic of Invert bits of a variable and it’s configurable from outside.
  • Many times we came across this situation where we want to invert all bits of an variable and than send it to DUT or use for checking purpose.
  • Here is how you do it..

`define width 4                      //Width whatever u want
…..
…..
bit [width-1:0] var  ;             // Variable that we need to invert
bit Invert;                            // This bit decide if 1’b0 -> keep is as it is , 1’b1 -> invert it
….
var = vlaue ;                        // Assign some value
var = var ^ {width{Invert}} ;     // MAIN LOGIC .. Just XOR our bit with same width as of our vaiable


Example :

Suppose value is 5 -> 0101 , width -> 4

  • If Invert bit is -> 1’b0 than  var = 0101 ^ 0000 = 0101      ( Same as input )
  • If Invert bit is -> 1’b1 than   var = 0101 ^ 1111    = 1010      ( Inverted output )

See It’s that simple..

You can configure Invert bit from outside..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: