Signed comparison in Verilog
Signed comparison in Verilog¶
When you write this in Verilog:
wire [7:0] a;
wire [7:0] b;
wire less;
assign less = (a < b);
the comparison between a and b is unsigned, that is a and b are numbers in the range 0-255. Writing this instead:
wire [7:0] a;
wire [7:0] b;
wire less;
assign less = ($signed(a) < $signed(b));
means that the comparison treats a and b as signed 8-bit numbers, which have a range of -128 to +127. Another way of writing the same thing is:
wire signed [7:0] a;
wire signed [7:0] b;
wire less;
assign less = (a < b);
A common operation on signed numbers is sign-extension. Here’s an easy way of doing it:
wire [7:0] a;
wire [15:0] ax; // a sign-extended to 16-bit
assign ax = {{8{a[7]}}, a};
This works by concatenating 8 copies of a’s sign bit with a itself.
While it’s legal to write comparisons between values with different sizes and signednesses, the rules are so confusing that you’re better off converting the arguments explicitly beforehand.
Signed comparison in Verilog的更多相关文章
- [转]Mac OS X local privilege escalation (IOBluetoothFamily)
Source: http://joystick.artificialstudios.org/2014/10/mac-os-x-local-privilege-escalation.html Nowad ...
- java.lang基础数据类型boolean、char、byte、short、int、long、float、double (JDK1.8)
java.lang.Boolean public static int hashCode(boolean value) { return value ? 1231 : 1237; } JDK 1.8新 ...
- java.lang.Byte 类源码浅析
Byte 类字节,属于Number. public final class Byte extends Number implements Comparable<Byte> { /** * ...
- java.lang.Long 类源码解读
总体阅读了Long的源码,基本跟Integer类类似,所以特别全部贴出源码,直接注释进行理解. // final修饰符 public final class Long extends Number i ...
- java.lang.Integer源码浅析
Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...
- Java集合类相关面试题
1.Collection和Collections的差别 java.util.Collection 是一个集合接口,Collection接口在Java类库中有非常多详细的实现.比如List.Set ja ...
- 沉淀再出发:java中的equals()辨析
沉淀再出发:java中的equals()辨析 一.前言 关于java中的equals,我们可能非常奇怪,在Object中定义了这个函数,其他的很多类中都重载了它,导致了我们对于辨析其中的内涵有了混淆, ...
- HDL代码风格建议(2)乘法器和DSP推断
Inferring Multipliers and DSP Functions Inferring Multipliers module unsigned_mult (out, a, b); :] o ...
- JVM Specification 9th Edition (3) Chapter 2. The Structure of the Java Virtual Machine
Chapter 2. The Structure of the Java Virtual Machine 内容列表 2.1. The class File Format (class文件的格式) 2. ...
随机推荐
- UML序列图总结(转)
序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色用一条虚线 ...
- erlang shell表格数据对齐
近期在erlang shell做一些測试,为了让測试结果数据显得更直观,想对齐须要打印的数据,做成像表格一样的效果. 開始的想法是在数据中插入tab. 当然,erlang也有对tab的支持,但实际效果 ...
- [置顶] ffmpg简介以及用它实现音频视频合并(java)
1.简介 FFmpeg是一个自由软件,可以运行音频和视频多种格式的录影.转档.流功能. 2.下载 源代码 git://git.libav.org/libav.git Windo ...
- URAL 1934 Black Spot --- 最短的简单修改
右侧是1.维护的同时保持最短路p值至少,我有直接存款(1-p).该概率不满足,为了使这个值极大. #include <iostream> #include <cstdlib> ...
- IIS6,IIS7中查看w3wp进程
当我们服务器创建了很多应用程序池,然后某个w3wp.exe进程占用CPU和内存过高,我们怎么查找这个w3wp.exe进程属于哪一个网站呢.其实微软为我们提供了很好的查看工具: 首先打开windows任 ...
- 调用API 清屏
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- HDU 4951 Multiplication table 阅读题
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4951 题意:给一个P进制的乘法表.行和列分别代表0~p-1,第i行第j*2+1和第j*2+2列代表的是第i ...
- ovs处理openflow消息的流程
OVS处理各个openflow消息的详细代码在 ofproto/ofproto.c 中: static enum ofperr handle_openflow__(struct ofconn *ofc ...
- [转载] C++11新特性
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
- Acdreamoj1115(数学思维称号)
意甲冠军:1,3是完美的数,假定a,b是完美的数,然后,2+a*b+2*a+2*b,结论认为,n无论是完美的数字. 解法:開始仅仅看出来2+a*b+2*a+2*b=(a+2)*(b+2)-2,没推出很 ...