在Java中声明一个unsigned int
本文翻译自:Declaring an unsigned int in Java
Is there a way to declare an unsigned int in Java? 有没有办法在Java中声明一个无符号的int?
Or the question may be framed as this as well: What is the Java equivalent of unsigned? 或也可以这样构想这个问题:Java的unsigned等同于什么?
Just to tell you the context I was looking at Java's implementation of String.hashcode()
. 只是为了告诉您上下文,我正在查看Java的String.hashcode()
。 I wanted to test the possibility of collision if the integer were 32 unsigned int. 我想测试整数是否为32 unsigned int时发生冲突的可能性。
#1楼
参考:https://stackoom.com/question/fLWA/在Java中声明一个unsigned-int
#2楼
You can use the Math.abs(number) function. 您可以使用Math.abs(number)函数。 It returns a positive number. 它返回一个正数。
#3楼
Java 8中有一个用于未签名的Integer和Long的API!
#4楼
Whether a value in an int is signed or unsigned depends on how the bits are interpreted - Java interprets bits as a signed value (it doesn't have unsigned primitives). 一个int值是带符号的还是无符号的,取决于位的解释方式-Java将位解释为带符号的值(它没有无符号的原语)。
If you have an int that you want to interpret as an unsigned value (eg you read an int from a DataInputStream that you know contains an unsigned value) then you can do the following trick. 如果您有一个要解释为无符号值的整数(例如,从知道包含无符号值的DataInputStream读取一个整数),则可以执行以下技巧。
int fourBytesIJustRead = someObject.getInt();
long unsignedValue = fourBytesIJustRead & 0xffffffffl;
Note, that it is important that the hex literal is a long literal, not an int literal - hence the 'l' at the end. 请注意,十六进制字面量是一个长字面量,而不是整数字面量是很重要的,因此末尾为“ l”。
#5楼
For unsigned numbers you can use these classes from Guava library : 对于无符号数字,您可以使用Guava库中的以下类:
- UnsignedInteger UnsignedInteger
- UnsignedLong 长无符号
They support various operations: 它们支持各种操作:
- plus 加
- minus 减去
- times 次
- mod 模
- dividedBy 除以
The thing that seems missing at the moment are byte shift operators. 目前似乎缺少的是字节移位运算符。 If you need those you can use BigInteger from Java. 如果需要这些,可以使用Java的BigInteger。
#6楼
We needed unsigned numbers to model MySQL's unsigned TINYINT
, SMALLINT
, INT
, BIGINT
in jOOQ , which is why we have created jOOU , a minimalistic library offering wrapper types for unsigned integer numbers in Java. 我们需要无符号数来建模jOOQ中 MySQL的无符号TINYINT
, SMALLINT
, INT
, BIGINT
,这就是为什么我们创建了jOOU的原因,这是一个简约的库,为Java中的无符号整数提供包装类型。 Example: 例:
import static org.joou.Unsigned.*;// and then...
UByte b = ubyte(1);
UShort s = ushort(1);
UInteger i = uint(1);
ULong l = ulong(1);
All of these types extend java.lang.Number
and can be converted into higher-order primitive types and BigInteger
. 所有这些类型都扩展了java.lang.Number
并且可以将其转换为高阶原始类型和BigInteger
。 Hope this helps. 希望这可以帮助。
(Disclaimer: I work for the company behind these libraries) (免责声明:我为这些库背后的公司工作)
在Java中声明一个unsigned int相关推荐
- java中怎么声明常量_如何在Java中声明一个常量
为了达到上述目的,您可以在Java 5及更高版本中使用enumtypes. 这是types安全的. A是一个实例variables. (如果它有静态修饰符,那么它就成为一个静态variables.)常 ...
- 是否可以在Gradle中声明一个可用于Java的变量?
本文翻译自:Is it possible to declare a variable in Gradle usable in Java? Is it possible to declare a var ...
- inferred type_您最终可以使用var在Java中声明Inferred Type局部变量-这就是为什么它很棒...
inferred type by javinpaul 由javinpaul 您最终可以使用var在Java中声明Inferred Type局部变量-这就是为什么它很棒 (You can finally ...
- 笹山希 java,c语言中unsigned int
C语言的unsigned int是什么意思? - 百度知道 最佳答案: C语言中unsigned int是无符号整数的意思. 无符号整型(unsigned int): (1)我们都知道整型是4个字节( ...
- java 全局数组_如何在Java中声明全局数组?
我有一个程序在Java中乘以两个矩阵.我在全局错误声明中发现了一些错误. 这里是我的代码如何在Java中声明全局数组? import java.util.Scanner; /**WAP in Java ...
- 怎么在java中关闭一个thread
怎么在java中关闭一个thread 我们经常需要在java中用到thread,我们知道thread有一个start()方法可以开启一个线程.那么怎么关闭这个线程呢? 有人会说可以用Thread.st ...
- java中判断一个字符在字符串中出现的次数
源代码: //java中判断一个字符出现的次数//在下面字符串中查找有几个啊public static void testFindChar(){String str = "啊!我爱你中国!啊 ...
- thread java 关闭_怎么在java中关闭一个thread
怎么在java中关闭一个thread 我们经常需要在java中用到thread,我们知道thread有一个start()方法可以开启一个线程.那么怎么关闭这个线程呢? 有人会说可以用Thread.st ...
- java整型转换为数组_基于java中byte数组与int类型的转换(两种方法)
java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送.者接收的数据都是 byte数组,但是int类型是4个byte组成的,如何把一个整形 ...
最新文章
- 在一家公司呆了 10 年的程序员,最后都怎么了?
- linux getty命令 开启 初始化 设置 终端 简介
- 5天5000万访问的个人网站是如何诞生的?
- 计算器软件----表达式求值
- WinForm实现简单的拖拽文件到出题的功能(C#)(3)
- linux yum安装mysql5.7_Linux安装MySQL5.7通过yum安装轻松搞定
- 手机电脑自适应导航源码php,自适应各终端懒人网址导航源码 v2.0
- Mysql like ' ' 会不会用到索引
- 力扣题目——102. 二叉树的层序遍历
- 使用python fabric搭建RHEL 7.2大数据基础环境以及部分优化
- Mac影音多媒体工具软件推荐
- JavaScript 计算标准体重的公式
- 光耦p621引脚图_p421光耦引脚图和代换
- 笔记本电脑计计算机硬盘分区,笔记本电脑如何分区,小编教你笔记本电脑如何分区...
- 清除某个特定网站的缓存---基于Chrome浏览器
- 喀纳斯与圣诞之夜(随想)
- ispreedSheet ios编辑类似表格界面在ipad上
- Android性能优化之内存优化浅析
- 虚拟主机还是云服务器玩游戏,虚拟主机还是云服务器玩游戏
- 手机号校验正则表达式