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. ...
随机推荐
- PageRank算法MapReduce实现
如果你现在需要计算网页的排名只有4一:数据如下面的: baidu 10.00 google,sina,nefu google 10.00 baidu sina 10.00 google nefu 10 ...
- HSQL
Whenever I connect to HSQLDB from my application deployed on eclipse Juno, it throws an exception as ...
- Jenkins(两)
官网:https://wiki.jenkins-ci.org/display/JENKINS/Meet+Jenkins 我的这篇文章不过简单的依据上文,介绍Jenkins提供了哪些功能.详细大家还是要 ...
- Prototype Pattern 原型模式
7.6 原型模式总结 原型模式作为一种快速创建大量相同或相似对象的方式,在软件开发中应用较为广泛,很多软件提供的复制(Ctrl + C)和粘贴(Ctrl + V)操作就是原型模式的典型应用,下面对该模 ...
- 玩转web之javaScript(五)---js和jquery一些不可不知的方法(input篇)
很多时候我们都利用js和jquery中操作input,比如追加属性,改变属性值等等,我在这里简单的整理了一下,并在以后逐步补充. 1:删除input的某一属性. <input name=&quo ...
- C++ map简单运用
#include <iostream> #include <string> #include <map> using namespace std; typedef ...
- Autofac 入门
Autofac 入门文档 原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: ...
- asp.net在用户控件中使用ClientScript
在用户空间中调用ClientScript.RegisterClientScriptBlock方法 ClientScript的命名空间是System.Web.UI.Page,并且要实例化之后的Page才 ...
- 交叉编译libxml2
请勿用于商业用途,转载请注明出处! xml的优势就是可以方便的管理配置项,libxml2是c语言实现的xml管理库,眼下项目须要ARM下的版本号,libxml2编译过程例如以下: 0.准备工作 下载地 ...
- UI —— 计算器
#import <UIKit/UIKit.h> @interface MyViewController :UIViewController { NSInteger _firstName; ...