# Swap bit

**URL:** <https://verificationacademy.com/forums/t/swap-bit/41421>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Verilog\
**Created:** [March 27, 2023, 7:39am UTC](https://verificationacademy.com/forums/t/swap-bit/41421 "2023-03-27T07:39:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveBhai](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@DaveBhai](https://verificationacademy.com/forums/u/DaveBhai)\
**Post date:** [March 27, 2023, 7:39am UTC](https://verificationacademy.com/forums/t/swap-bit/41421/1 "2023-03-27T07:39:23Z")

</div>

Hello My que. is I have variable a which is int. and direct give value of 'h2431\_6578;  
which I have to arrange in ascending order. I blindly know that to arrange this by streaming operator I did use it at once but,I stucked to arrange.So,kidly help to resolve this que.

Need to answer as 'h12345678.

---

<div class="post-metadata">

**Author:** ![KillSteal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@KillSteal](https://verificationacademy.com/forums/u/KillSteal)\
**Post date:** [March 27, 2023, 5:44pm UTC](https://verificationacademy.com/forums/t/swap-bit/41421/2 "2023-03-27T17:44:09Z")

</div>

_In reply to [Davebhai](https://verificationacademy.com/forums/t/swap-bit/41421):_

I used the below function. I haven’t tried streaming operators. We could use constraints as well; but I haven’t tried it yet.

```verilog

// Code your testbench here
// or browse Examples
module test();
  
  int a = 32'h2431_6578;
  int b;
  
  initial begin
    b = sort_64bit(a);
    $display("a=%0h",a);
    $display("b=%0h",b);
    
  end
  
  
  
  function int sort_64bit(input int l_input);
    bit [3:0] temp[$];
    
    foreach(l_input[i]) begin
      if((i%4)==0) temp.push_back(l_input[i+:4]);
    end
    
    temp.sort();
    temp.reverse();

    
    foreach(temp[i]) begin
      sort_64bit[(i*4)+:4] = temp[i];
    end
    
  endfunction
 
endmodule

```

---

<div class="post-metadata">

**Author:** ![DaveBhai](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@DaveBhai](https://verificationacademy.com/forums/u/DaveBhai)\
**Post date:** [March 29, 2023, 7:19am UTC](https://verificationacademy.com/forums/t/swap-bit/41421/3 "2023-03-29T07:19:54Z")

</div>

_In reply to [KillSteal](https://verificationacademy.com/forums/t/swap-bit/41421/2):_

Thank you so much Brother.
