SystemVerilog Streaming Operator: Pack, Unpack and Byte Swap (Tested)
In short: The SystemVerilog streaming operator turns data into a stream of bits and back. {>>{a, b}} streams left to right, and {<<{a, b}} streams right to left in slices (1 bit by default). It packs on the right side of = and unpacks on the left, and {<<8{word}} swaps the byte order of a word, which is why drivers and monitors use it to serialize packets.
Every driver eventually turns a transaction into bytes, and every monitor turns bytes back into a transaction. You can write that with loops and bit slices, or in one line with the streaming operator.
In this lesson we test 9 streaming checks in one UVM bench, ending with a packet that packs itself into bytes and unpacks back. I ran every check on Synopsys VCS, and one of them gave a different answer on another simulator.
What you will learn
- How
>>and<<decide the order of the stream. - What the slice size does, and why it changes nothing on
>>. - How to swap bytes, reverse bits and swap nibbles in one line.
- How to pack a transaction into a dynamic array of bytes and unpack it with
with. - The mistakes that give wrong bytes without any error.
Cheat sheet: streaming operator in 6 lines
| You want | Write | Result on VCS |
|---|---|---|
| Pack fields into one vector | v = {>>{a, b, c}}; | Same as {a, b, c} when the widths match |
| Unpack a vector into fields | {>>{a, b, c}} = v; | Fields take bits from the left |
| Byte swap (endian change) | {<<8{word}} | 32'h11223344 becomes 32'h44332211 |
| Bit reverse | {<<{x}} | 8'b1100_0001 becomes 8'b1000_0011 |
| Nibble swap | {<<4{x}} | 8'hAB becomes 8'hBA |
| Packet to bytes and back | bytes = {>>8{addr, len, payload, crc}}; | 7 bytes out, the same packet back |
How >> and << decide the order
| Operator | Order | What the slice size does |
|---|---|---|
{>> N {list}} | Left to right, as written | Nothing visible. Slices still go out left to right |
{<< N {list}} | Right to left | Sets the chunk size that gets reversed. Bits inside a slice keep their order |
- Slice size can be a number (
8) or a type (byte,int).{<<byte{x}}is the same as{<<8{x}}. - Default slice size is 1 bit, so
{<<{x}}reverses every bit. - Pack or unpack depends only on the side of the
=.
>>8 keeps the order, <<8 reverses the slices but not the bits inside them.Analogy: think of reading a long word in chunks. >> reads the chunks from the first letter to the last. << reads the chunks from the last to the first, but each chunk is still read forwards, and the chunk size is the slice size.
Pack, unpack and the with clause
- Pack: the streaming expression is on the right of
=. A dynamic array target is sized to fit. - Wider fixed target: the stream is left-aligned and zeros fill the right. On VCS,
bit [47:0] w = {>>{16'hBEEF}};gave48'hBEEF_0000_0000. - Unpack: the streaming expression is on the left of
=. Fields take bits from the left of the source, in the order you list them. - The with clause:
payload with [0 +: len]tells the unpack how many items of a dynamic array to take.lencan be a field unpacked earlier in the same expression, and VCS accepted exactly that. - Operands: integral types, packed and unpacked arrays and structs of them, dynamic arrays and queues.

len is unpacked first, so with [0 +: len] knows to take 3 payload bytes.Streaming operator vs a manual loop
| Streaming operator | Manual loop | |
|---|---|---|
| Code size | One line | 5 to 15 lines |
| Field order | Exactly as listed | Easy to get one index wrong |
| Variable-length payload | with [0 +: len] | Loop bound from len |
| Best for | Serialize and deserialize whole transactions | Odd bit layouts, a CRC computed on the fly |
The working example: 9 checks in one UVM bench
I ran this exact file on Synopsys VCS X-2025.06 with UVM 1.2. Every user name uses the tav_ prefix and every message goes through uvm_info.
Run it yourself: the same file is on GitHub. Paste it into testbench.sv on EDA Playground, leave design.sv empty, pick VCS and UVM 1.2, and set the top module to tav_top.
| Check | Expression | VCS result |
|---|---|---|
| 1a | {>>{32'h11223344}} | 32'h11223344 |
| 1b | {<<{32'h11223344}} (bit reverse) | 32'h22CC4488 |
| 2 | {<<8{32'h11223344}} (byte reverse) | 32'h44332211 |
| 3 | {<<{8'b1100_0001}} | 8'h83 (8'b1000_0011) |
| 4 | {<<4{8'hAB}} (nibble swap) | 8'hBA |
| 5 | {>>8{32'h11223344}} | 32'h11223344 (no change) |
| 6 | 48-bit target = {>>{16'hBEEF}} | 48'hBEEF00000000 |
| 7a | Packet packed to bytes | 7 bytes: a5 c3 03 10 20 30 7e |
| 7b | Round trip into a new packet | Same as the original |
Planted bug: +BUG=1 unpacks crc before payload. Every size still fits, so the simulator raises no error, and only the round trip check catches it.
// tav_stream_uvm.sv : the SystemVerilog streaming operator (pack and unpack), a complete working example (UVM 1.2)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 30 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// What is in here:
// tav_pkt a UVM transaction (addr, len, payload[], crc) with pack_bytes() and unpack_bytes()
// written with the streaming operator, the way a driver and monitor serialize traffic
// tav_stream_test seven checks, each printed with `uvm_info and checked with `uvm_error:
// 1 >> vs << 2 byte reverse 3 bit reverse 4 nibble swap
// 5 slice size on >> 6 pack into a wider target 7 packet round trip
//
// Run options:
// (none) all checks pass, 0 errors
// +BUG=1 unpack_bytes() reads crc before payload (wrong field order), so the round trip fails
`include "uvm_macros.svh"
package tav_stream_pkg;
import uvm_pkg::*;
`include "uvm_macros.svh"
// ---------------------------------------------------------------
// A packet that turns itself into bytes and back
// ---------------------------------------------------------------
class tav_pkt extends uvm_sequence_item;
`uvm_object_utils(tav_pkt)
bit [15:0] addr;
bit [7:0] len;
bit [7:0] payload[];
bit [7:0] crc;
int tav_bug;
function new(string name = "tav_pkt");
super.new(name);
endfunction
// Pack: fields go out left to right, 8 bits at a time.
// The target is a dynamic array, so it is sized to fit: 2 + 1 + len + 1 bytes.
function void pack_bytes(output bit [7:0] bytes[]);
bytes = {>>8{addr, len, payload, crc}};
endfunction
// Unpack: the same order. "with [0 +: len]" tells the stream how many
// payload bytes to take, using len, which is unpacked just before it.
function void unpack_bytes(input bit [7:0] bytes[]);
if (tav_bug == 1)
{>>8{addr, len, crc, payload with [0 +: len]}} = bytes; // BUG: crc read before payload
else
{>>8{addr, len, payload with [0 +: len], crc}} = bytes;
endfunction
function bit tav_same(tav_pkt rhs);
if (addr != rhs.addr || len != rhs.len || crc != rhs.crc) return 0;
if (payload.size() != rhs.payload.size()) return 0;
foreach (payload[i]) if (payload[i] != rhs.payload[i]) return 0;
return 1;
endfunction
function string convert2string();
string s = $sformatf("addr=0x%04h len=%0d payload=", addr, len);
foreach (payload[i]) s = {s, $sformatf("%02h ", payload[i])};
return {s, $sformatf("crc=0x%02h", crc)};
endfunction
endclass
// ---------------------------------------------------------------
// The test: seven small checks
// ---------------------------------------------------------------
class tav_stream_test extends uvm_test;
`uvm_component_utils(tav_stream_test)
int tav_pass, tav_fail, tav_bug;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
void'($value$plusargs("BUG=%d", tav_bug));
endfunction
function void tav_check(string what, logic [63:0] got, logic [63:0] exp);
if (got === exp) begin
tav_pass++;
`uvm_info("TAV_STREAM", $sformatf("PASS %s: got 0x%0h", what, got), UVM_LOW)
end
else begin
tav_fail++;
`uvm_error("TAV_STREAM", $sformatf("FAIL %s: got 0x%0h, expected 0x%0h", what, got, exp))
end
endfunction
task run_phase(uvm_phase phase);
bit [31:0] word = 32'h1122_3344;
bit [31:0] r32;
bit [7:0] b8 = 8'b1100_0001;
bit [7:0] r8;
bit [47:0] wide;
phase.raise_objection(this);
// 1. >> keeps the order, << reverses it (slice size 1 bit by default)
r32 = {>>{word}};
tav_check("1a {>>{32'h11223344}}", r32, 32'h1122_3344);
r32 = {<<{word}};
tav_check("1b {<<{32'h11223344}} (bit reverse)", r32, 32'h22CC_4488);
// 2. << with a slice size of 8 reverses the bytes: endian swap
r32 = {<<8{word}};
tav_check("2 {<<8{32'h11223344}} (byte reverse)", r32, 32'h4433_2211);
// 3. Bit reverse of one byte
r8 = {<<{b8}};
tav_check("3 {<<{8'b1100_0001}} (bit reverse)", r8, 8'b1000_0011);
// 4. Nibble swap: slice size 4
r8 = {<<4{8'hAB}};
tav_check("4 {<<4{8'hAB}} (nibble swap)", r8, 8'hBA);
// 5. A slice size on >> changes nothing: slices still go left to right
r32 = {>>8{word}};
tav_check("5 {>>8{32'h11223344}}", r32, 32'h1122_3344);
// 6. Packing into a wider target: the stream is left-aligned, zeros fill the right
wide = {>>{16'hBEEF}};
tav_check("6 48-bit target = {>>{16'hBEEF}}", wide, 48'hBEEF_0000_0000);
// 7. Packet round trip: pack to bytes, unpack into a new packet, compare
begin
tav_pkt tx = tav_pkt::type_id::create("tx");
tav_pkt rx = tav_pkt::type_id::create("rx");
bit [7:0] bytes[];
string s;
tx.addr = 16'hA5C3;
tx.len = 3;
tx.payload = '{8'h10, 8'h20, 8'h30};
tx.crc = 8'h7E;
tx.pack_bytes(bytes);
foreach (bytes[i]) s = {s, $sformatf("%02h ", bytes[i])};
`uvm_info("TAV_STREAM", $sformatf("7 tx %s", tx.convert2string()), UVM_LOW)
`uvm_info("TAV_STREAM", $sformatf("7 packed %0d bytes: %s", bytes.size(), s), UVM_LOW)
tav_check("7a packed byte count", bytes.size(), 7);
rx.tav_bug = tav_bug;
rx.unpack_bytes(bytes);
`uvm_info("TAV_STREAM", $sformatf("7 rx %s", rx.convert2string()), UVM_LOW)
tav_check("7b round trip (1 = same)", rx.tav_same(tx), 1);
end
`uvm_info("TAV_STREAM", $sformatf("Checks: %0d pass, %0d fail", tav_pass, tav_fail), UVM_NONE)
phase.drop_objection(this);
endtask
endclass
endpackage
module tav_top;
import uvm_pkg::*;
import tav_stream_pkg::*;
initial run_test("tav_stream_test");
endmodule
What the run shows
What to notice:
- Checks 1 to 5: only
<<changes the order, and the slice size decides what gets reversed. - Check 6: the 16-bit value lands in the top bits of the 48-bit target.
- Check 7: the packet packs to exactly 7 bytes and comes back identical.
- Result: 9 pass, 0 fail, 0
UVM_ERROR.
Here is the clean run from VCS:
UVM_INFO @ 0: reporter [RNTST] Running test tav_stream_test...
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 1a {>>{32'h11223344}}: got 0x11223344
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 1b {<<{32'h11223344}} (bit reverse): got 0x22cc4488
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 2 {<<8{32'h11223344}} (byte reverse): got 0x44332211
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 3 {<<{8'b1100_0001}} (bit reverse): got 0x83
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 4 {<<4{8'hAB}} (nibble swap): got 0xba
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 5 {>>8{32'h11223344}}: got 0x11223344
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 6 48-bit target = {>>{16'hBEEF}}: got 0xbeef00000000
UVM_INFO testbench.sv(143) @ 0: uvm_test_top [TAV_STREAM] 7 tx addr=0xa5c3 len=3 payload=10 20 30 crc=0x7e
UVM_INFO testbench.sv(144) @ 0: uvm_test_top [TAV_STREAM] 7 packed 7 bytes: a5 c3 03 10 20 30 7e
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 7a packed byte count: got 0x7
UVM_INFO testbench.sv(148) @ 0: uvm_test_top [TAV_STREAM] 7 rx addr=0xa5c3 len=3 payload=10 20 30 crc=0x7e
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 7b round trip (1 = same): got 0x1
UVM_INFO testbench.sv(152) @ 0: uvm_test_top [TAV_STREAM] Checks: 9 pass, 0 fail
--- UVM Report Summary ---
UVM_INFO : 16
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0
And the key lines with +BUG=1. The payload shifts by one byte and crc comes back as 0x10:
UVM_INFO testbench.sv(143) @ 0: uvm_test_top [TAV_STREAM] 7 tx addr=0xa5c3 len=3 payload=10 20 30 crc=0x7e
UVM_INFO testbench.sv(144) @ 0: uvm_test_top [TAV_STREAM] 7 packed 7 bytes: a5 c3 03 10 20 30 7e
UVM_INFO testbench.sv(89) @ 0: uvm_test_top [TAV_STREAM] PASS 7a packed byte count: got 0x7
UVM_INFO testbench.sv(148) @ 0: uvm_test_top [TAV_STREAM] 7 rx addr=0xa5c3 len=3 payload=20 30 7e crc=0x10
UVM_ERROR testbench.sv(93) @ 0: uvm_test_top [TAV_STREAM] FAIL 7b round trip (1 = same): got 0x0, expected 0x1
UVM_INFO testbench.sv(152) @ 0: uvm_test_top [TAV_STREAM] Checks: 8 pass, 1 fail
UVM_ERROR : 1
One check, two simulators, two answers
Before the VCS run, I tried checks 1 to 6 in Verilator 5.020. Five matched, but check 6 did not: Verilator gave 48'h0000_0000_BEEF, with the value on the right. VCS gave 48'hBEEF_0000_0000, with the value on the left and zeros filling the right, which is what the bench expected.
Verilator 5.020 also refused with [0 +: len] as unsupported, so check 7 could not run there at all.
The lesson: streaming edge cases are where simulators differ. If your code relies on one, like packing into a wider target, add a self-check for it and run it on every simulator your team uses.
Common mistakes to avoid
| Mistake | What you see | Fix |
|---|---|---|
Using >>8 to swap bytes | Nothing changes | Use <<8: only << reverses |
Using << with no slice size to swap bytes | Every bit reversed, not the bytes | {<<8{x}} for bytes, {<<{x}} for bits |
| Unpacking fields in a different order than packing | Wrong values, no simulator error (+BUG=1) | Pack and unpack with the same field list |
No with on a variable-length payload | The payload takes the wrong number of bytes | payload with [0 +: len] |
| Packing into a wider fixed target and expecting the value on the right | The value lands in the top bits, zeros below | Size the target to the stream, or use a dynamic array |
| Assuming every simulator agrees | Different results on edge cases (check 6) | Self-check the edge case on each tool |
How AI helps you master this
- Predict before you run: paste a streaming expression and ask for the result in hex, then compare with the simulator. It is a fast way to learn
<<slicing. - Generate pack and unpack pairs: give it your transaction fields and ask for
pack_bytes()andunpack_bytes()with the same order. - Review for order bugs: paste both functions and ask whether the field lists match.
Warning: a convincing AI answer is not the same as a correct engineering result. AI tools often get << with a slice size wrong, for example reversing the bits inside each byte. Check every answer on the simulator.
Interview questions on the streaming operator
- What is the difference between
>>and<<?>>streams left to right.<<streams right to left in slices of the given size. - How do you swap the byte order of a 32-bit word?
{<<8{word}}. - Does a slice size change a
>>stream? No. Slices still go out in order, so the result is the same. - How do you unpack a variable-length payload? Put the length field first and use
payload with [0 +: len].
More practice: SystemVerilog interview questions.
Practice challenge
- Run the bench and confirm 9 pass, 0 fail.
- Run with
+BUG=1and explain whycrccomes back as0x10. - Predict
{<<16{32'h11223344}}, then add it as check 8. - Bonus: add a 16-bit
tagfield afteraddr. What must change in both functions, and what is the new byte count?
FAQ
What is the streaming operator in SystemVerilog? An operator, {>>{...}} or {<<{...}}, that packs data into a stream of bits or unpacks a stream back into variables.
What is the difference between pack and unpack? Pack is the streaming expression on the right of =. Unpack is the streaming expression on the left of =.
What does the slice size mean in {<<8{data}}? It is the chunk size that << reverses. With 8, the bytes are reversed and the bits inside each byte keep their order.
How do I reverse byte order in SystemVerilog? Use {<<8{data}}. For bits, use {<<{data}}.
Can the streaming operator work with dynamic arrays and queues? Yes. A dynamic array target is sized to fit when packing, and with [0 +: n] sets how many items to take when unpacking.
When should I use a manual loop instead? When the layout is not a simple field list, such as a CRC computed while you serialize.
References
- IEEE 1800-2017, SystemVerilog standard: section 11.4.14, streaming operators (pack and unpack).
Keep learning
- Arrays first: arrays and queues in SystemVerilog.
- Copying objects: shallow copy vs deep copy.
- Where packing is used: UVM sequencer and driver communication.
- Interview prep: SystemVerilog interview questions.
Part of the learning path SystemVerilog for verification
