unsigned和std_logic_vector之间的区别 [英] Difference between unsigned and std_logic_vector

查看:233
本文介绍了unsigned和std_logic_vector之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我下面书面声明之间的区别.

can anyone tell me about the difference between below written statement.

signal A: **unsigned**(3 downto 0);
signal B: **std_logic_vector**(3 downto 0);

推荐答案

std_logic_vectorunsigned都是>的无约束数组.与signed类型一样. std_logic_vectorstd_logic_1164包中声明; unsignedsigned在包numeric_std中声明.这三种类型都是相同的.唯一的区别是他们的名字.

Both std_logic_vector and unsigned are unconstrained arrays of std_logic. As is the signed type. std_logic_vector is declared in the std_logic_1164 package; unsigned and signed are declared in the package numeric_std. All three types are identical; the only difference is their names.

那么,这有什么意义呢?一个例子很好地说明了这一点:

So, what's the point of that? The point is well illustrated by an example:

使用

variable U : unsigned(3 downto 0);
variable S : signed(3 downto 0);
variable I : integer;

然后

U := "1111";
I := to_integer(U);

I的值设置为15,而

S := "1111";
I := to_integer(S);

导致I的值为-1.这是因为unsigned类型用于表示 unsigned 数字,该数字只能为正数.因此,"1111"表示数字15.但是,signed类型也必须能够表示负数,并且对于signed类型,"1111"表示-1(因为二进制补码表示使用此类型).

results in I being given the value -1. This is because the unsigned type is used to represent an unsigned number, which can only be positive. So, "1111" represents the number 15. A signed type, however, needs to be able to represent negative numbers, too, and with the signed type "1111" represents -1 (because the twos complement representation is used by this type).

因此,您可以看到相同的函数-to_integer-用"1111"调用时返回两个不同的结果-15还是-1,具体取决于参数是unsigned还是signed类型.因此,即使两种名称之间唯一的区别是它们的名称,您也可以看到两种类型的区别.

So, you can see that the same function - to_integer - returns two different results when called with "1111" - either 15 or -1 depending on whether the argument is of type unsigned or signed. So, you can see the point in having both types, even though the only difference between them is their name.

实际上,有两个to_integer函数,而不是一个.一个带有unsigned参数.另一个(标识为to_integer)采用一个signed参数.如您所见,它们的行为确实有所不同.编译器可以根据参数的类型来决定需要调用哪个函数.这种想法可以让编译器根据参数的类型在不同的(但名称相同的函数)之间进行选择,称为 overloading .在软件语言中很常见.

Actually, there are two to_integer functions, not one. One takes an unsigned argument; the other (identically named to_integer) takes a signed argument. As you can see, they do behave differently. The compiler can decide which function needs to be called based on the type of the argument. This idea, where a compiler can choose between different (but identically-named functions) based on the type of the argument, is called overloading. It is common in software languages.

那么,std_logic_vector呢?假设您写了:

So, what about std_logic_vector? Suppose you wrote:

variable V : std_logic_vector(3 downto 0);
variable I : integer;

然后

V:= "1111";
I := to_integer(V);

您希望从to_integer函数得到什么结果? 15还是-1?上面的代码是非法的,这个难题得以解决-它不会编译.它不会编译,因为没有为std_logic_vector定义的to_integer函数版本-对于std_logic_vector类型,to_integer函数不会重载.

what result would you expect from the to_integer function? 15 or -1? This dilemma is solved by the above code being illegal - it won't compile. It won't compile, because there is no version of the to_integer function defined for std_logic_vector - the to_integer function is not overloaded for the type std_logic_vector.

因此,如果您只需要表示正数,则最好使用unsigned类型;否则,请使用unsigned类型.如果需要表示负数,则需要使用signed类型.如果您不太在意,因为您的位模式不是数字,或者因为您没有对它进行任何数学运算(您只是将其从一个地方传输到另一个地方),那么最好使用std_logic_vector

So, if you only need to represent positive numbers, you're best off using the unsigned type; if you need to represent negative numbers you need to use the signed type. If you don't really care, because your pattern of bits is not a number or because your not doing any maths on it (you're merely transporting it from one place to another), then you're best off using std_logic_vector.

https://www.edaplayground.com/x/2Qq4

这篇关于unsigned和std_logic_vector之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆