## Featured post

### Transaction Recording In Verilog Or System Verilog

As there is not yet a standard for transaction recording in Verilog or VHDL, ModelSim includes a set of system tasks to perform transac...

## Sunday, 9 September 2012

### Expression Bit Length

Formal Definition

This chapter explains values for evaluation of size of expressions.

Description

Every expression has a result with a specified bit length. Therefore operand lengths should be checked before the expression is calculated to prevent the loss of important bits. This problem occurs when the result of an expression is assigned to a variable that cannot store all the result bits.

 Expression Bit length Unsized constant number Same as integer Sized constant number As given i op j, op: + - / % & | ^ ^~ ~^ Max(length of i, length of j) i * j Length of i + length of j op i, op: + - ~ Length of i i op j, op: == != === !== && || > >= < <= 1 bit op i, op: & ~& | ~| ^ ~^ ^~ 1 bit i op j, op: >> << Length of i i ? j : k Max(length of j, length of k) {i,j} Length of i + length of j {i{j,k}} i * (length of j + length of k)

Table 2 Expression bit length rules

Examples

Example 1

reg [3:0] a;
reg [7:0] b;
reg [15:0] c;
a + b -> 8 bits
a * b -> 12 bits
a || b -> 1 bit
a >> 2 -> 4 bits
(c) ? a : b -> 8 bits
{i,j} -> 12 bits
{2(a,b}} -> 24 bits

Important Notes

• The expression result bit-length should be known before the assignment to prevent unintended data loss.

#### Post a comment

Please provide valuable comments and suggestions for our motivation. Feel free to write down any query if you have regarding this post.