Verilog numerical calculation-signed number and unsigned number __verilog

Source: Internet
Author: User
Tags arithmetic truncated
Verilog Numerical calculation-signed numbers and unsigned numbersBit width truncation arithmetic operations

Signed number assignment

Bit width truncation
A bit of a large number assigned to the bit width of the small number, the data will be truncated, the truncation rule is from the low start, truncated is the high

Code

Wire [5:0] A;
Wire [4:0] f;
Localparam data1=5 ' b11001,
           data2=5 ' b11010;
Assign A= $signed (data1) + $signed (data2);
Assign F=a;

Result
Arithmetic operations

The numerical calculation contains three kinds of cases: Two signed number operations, two unsigned number operations, signed number operations and unsigned number operations.
Only two operands are signed, and the two operands are counted as signed numbers, otherwise either signed or unsigned numbers are counted according to unsigned numbers.
Code

Wire [5:0] a,b,c;
Localparam data1=5 ' b11001,
           data2=5 ' b11010;
Assign A= $signed (data1) + $signed (data2);
Assign b= $unsigned (data1) + $unsigned (data2);
Assign c= $signed (data1) + $unsigned (DATA2);

Result
Signed number assignment

Assign a number or result a (signed or unsigned) to another number B, depending on the bit width, there are three different cases:
When the bit width is the same, the direct assignment, no matter B is signed or unsigned, has no effect on the result of the assignment, just the bits of the total invariant value.
When the bit width of B is smaller, the bit width is truncated, as mentioned above, to intercept from the low
When the bit width of B is larger than a, the binary code of a is first filled in the low position of B, and the high filling is divided into three kinds of cases:
If the a,b is a signed number, the high position of B is filled with the top of a
If a,b are unsigned, the high of B is filled with 0
If A is a signed number, B is an unsigned number, and the high position of B is filled with the top of a
If A is unsigned, B is a signed number, and B's high is filled with 0
Summary: If A is a signed number, then B high free with a the highest bit of padding, if a is unsigned number, B's high is filled with 0
Code

Wire [5:0] A;
Wire [7:0] a1,a2;
Wire signed [7:0] A3,A4;
Localparam data1=5 ' b11001,
           data2=5 ' b11010;
Assign A= $signed (data1) + $signed (data2);
assign A1=a;
Assign A2= $signed (a);
assign A3=a;
Assign a4= $signed (a);

Result

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.