java java中unsigned long的等价物是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6742250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the equivalent of unsigned long in java
提问by Pramod
i have written these following three functions for my project to work:
我已经为我的项目编写了以下三个函数:
WORD shuffling(WORD x)
{
// WORD - 4 bytes - 32 bits
//given input - a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15- b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15
//output required - a0,b0,a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6,a7,b7 - a8,b8,a9,b9,a10,b10,a11,b11,a12,b12,a13,b13,a14,b14,a15,b15
x = (x & 0X0000FF00) << 8 | (x >> 8) & 0X0000FF00 | x & 0XFF0000FF;
x = (x & 0X00F000F0) << 4 | (x >> 4) & 0X00F000F0 | x & 0XF00FF00F;
x = (x & 0X0C0C0C0C) << 2 | (x >> 2) & 0X0C0C0C0C | x & 0XC3C3C3C3;
x = (x & 0X22222222) << 1 | (x >> 1) & 0X22222222 | x & 0X99999999;
return x;
}
WORD t_function(WORD n)
{
WORD t_result=0;
WORD64 var = 2*((n*n)& 0xFFFFFFFF)+n; // (n*n mod FFFFFFFF) becomes a 32-bit word
t_result = (WORD) ((var)& 0xFFFFFFFF);
return t_result;
}
WORD lfsr(WORD t_result)
{
WORD returnValue = t_result;
WORD flag = 0;
flag = returnValue & 0x80000000; // Checking if MSB is 1 or 0
// Left shift the input
returnValue = returnValue << 1;
// If MSB is 1 then XOR the reult with the primitive polynomial
if(flag > 0)
{
returnValue = returnValue ^ 0x4C11DB7;
}
return returnValue;
}
WORD - unsigned long
WORD - 无符号长
this code is in "c". Now i have to implement this in java. Everything is fine in compiling and running the code. But here i used unsigned long and in java i have used int Since i am operating on 32bits at a time. The problem is "when implementing in java whenever the result is going out of range of int the output is being deviated and it will not be the same output from the c code. Is there any solution for my problem to replace the unsigned long range values in java
此代码在“c”中。现在我必须在java中实现它。编译和运行代码一切正常。但是在这里我使用了 unsigned long 并且在 Java 中我使用了 int 因为我一次操作 32 位。问题是“当在 java 中实现时,只要结果超出 int 的范围,输出就会偏离,并且它不会与 c 代码的输出相同。是否有任何解决方案可以解决我的问题来替换无符号长范围值在 Java 中
回答by Basil Bourque
Update – Java 8 Has Unsigned int
& long
更新 – Java 8 已未签名int
&long
Originally in Java, the primitive integer data types (byte, short, int, and long) were signed (positive or negative).
最初在 Java 中,原始整数数据类型(byte、short、int 和 long)是有符号的(正数或负数)。
Now I see in the Java Tutorialthat as of Java SE 8, both int
and long
can be used as unsigned.
现在我在Java 教程中看到,从 Java SE 8 开始,int
和long
都可以用作未签名。
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the
Integer
classto support the arithmetic operations for unsigned integers.long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2?3 and a maximum value of 2?3-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2??-1. The unsigned long has a minimum value of 0 and maximum value of 2??-1. Use this data type when you need a range of values wider than those provided by int. The
Long
classalso contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
int:默认情况下,int 数据类型是一个 32 位有符号二进制补码整数,其最小值为 -231,最大值为 231-1。在 Java SE 8 及更高版本中,您可以使用 int 数据类型来表示一个无符号的 32 位整数,其最小值为 0,最大值为 232-1。使用 Integer 类将 int 数据类型用作无符号整数。有关更多信息,请参阅数字类部分。
Integer
类中添加了诸如 compareUnsigned、divideUnsigned 等静态方法,以支持无符号整数的算术运算。long:long 数据类型是一个 64 位二进制补码整数。有符号 long 的最小值为 -2?3,最大值为 2?3-1。在 Java SE 8 及更高版本中,您可以使用 long 数据类型来表示无符号的 64 位 long,其最小值为 0,最大值为 2??-1。unsigned long 的最小值为 0,最大值为 2??-1。当您需要比 int 提供的值范围更广的值时,请使用此数据类型。该
Long
班还包含了诸如compareUnsigned方法,divideUnsigned等长,以支持算术运算的无符号。
I am notnecessarily recommending this approach. I'm merely making you aware of the option.
我并不一定推荐这种做法。我只是让你知道这个选项。
回答by LeleDumbo
Short answer, there's no unsigned data type in java. long in C is 32-bit on 32-bit systems, but java's long is 64-bit, so you can use that for replacement (at least it would solve the overflow problem). If you need even wider integers, use BigInteger class.
简短的回答,java 中没有无符号数据类型。C 中的 long 在 32 位系统上是 32 位的,但是 java 的 long 是 64 位的,所以你可以用它来替换(至少它可以解决溢出问题)。如果您需要更宽的整数,请使用 BigInteger 类。
回答by Ryan Stewart
Look over Java's Primitive Data Types. If you need something bigger than a long, try a BigInteger.
查看 Java 的原始数据类型。如果您需要比 long 更大的东西,请尝试BigInteger。