Wednesday, July 22, 2020

How to Swap a two variable with and without using temporary variable in System Verilog

Hi Everyone !!!!,
                  In this session we are going to see the methods of swapping a two variables with and without using temporary variables in System Verilog.

Method 1:
                  By using non blocking statement.
                                  
                                      module TB;
                                           int a=10,b=15;
                                           initial begin 
                                              a<=b;
                                              b<=a;
                                               $monitor(" Values of a=%0d b=%0d",a,b);
                                           end
output:
                         Values of a=15 b=10

Note:
      Non blocking statements executes parallelly  and assign the values at the end of current time step. We will see brief info about blocking and non blocking in further blogs.

Method 2:
                  By using xor operators.

                                        module TB;
                                           int a=10,b=15;
                                           initial begin 
                                              a=a^b;  // a=(1010)^(1111)->0101
                                              b=a^b; //b=(0101)^(1111)=1010->10
                                              a=a^b; //a=(0101)^(1010)=1111->15
                                              $display(" Values of a=%0d b=%0d",a,b);
                                        end
output:
                         Values of a=15 b=10 

Method 3:

                       By using addition and subtraction operators. But it won't support for signed numbers.

                                        module TB;
                                           int a=10,b=15;
                                           initial begin 
                                              a=a+b;  // a=25
                                              b=a-b;   //b=25-15->10
                                              a=a-b;   //a=25-10->15
                                              $display(" Values of a=%0d b=%0d",a,b);
                                        end
output:
                         Values of a=15 b=10 

Method 4:
                       By using  multiplication and division operators.

                                        module TB;
                                           int a=10,b=15;
                                           initial begin 
                                              a=a*b;  // a=150
                                              b=a/b;  //b=150/15->10
                                              a=a/b;  //a=150/10->15
                                              $display(" Values of a=%0d b=%0d",a,b);
                                        end
output:
                         Values of a=15 b=10 

Method 5:

                    By using temporary variable.
                                
                                       module TB;
                                           int a=10,b=15, temp;
                                           initial begin 
                                              temp=a;
                                                    a=b;
                                                    b=temp;
                                              $display(" Values of a=%0d b=%0d",a,b);
                                        end
output:
                                  Values of a=15 b=10 


Hope this logics will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM   

"The world breaks everyone, and afterward, Some are strong at the broken places." 
                                                                                                         
                                                                                        -Ernest Hemingway 

4 comments:

  1. Wonderful attempt of trying to simply ptherwoth hard concepts.
    Truly admirable.
    Keep up this amazing work.
    Absolutely appreciate your effort wholeheartedly.

    ReplyDelete
  2. Wonderful attempt of trying to simplify otherwise hard concepts.
    Truly admirable.
    Keep up this amazing work.
    Absolutely appreciate your effort wholeheartedly.

    ReplyDelete

`define Macro usage in System Verilog

 Hi Everyone !!! In this blog we are going to see the usage of `define macro in System Verilog. A text macro substitution facility has been ...