gpt4 book ai didi

java - java中unsigned long的等价物是什么

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:38:50 26 4
gpt4 key购买 nike

我为我的项目编写了以下三个函数:

 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 - 无符号长整型

此代码在“c”中。现在我必须在 java 中实现它。编译和运行代码一切正常。但是在这里我使用了 unsigned long 而在 java 中我使用了 int 因为我一次在 32 位上运行。问题是“当结果超出 int 范围时在 java 中实现时,输出会发生偏差,并且它不会与 c 代码的输出相同。我的问题是否有任何解决方案来替换无符号长范围值在Java中

最佳答案

更新 – Java 8 可以将已签名的 intlong 视为未签名

在 Java 中,原始整数数据类型(byteshortintlong)是signed (正面或负面)。

从 Java 8 开始,intlong 都可以被显式地视为无符号。现在正式成为一项功能,但仍然是一种黑客攻击。有些人可能会发现它在某些有限的情况下很有用。查看Java Tutorial .

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2³¹ and a maximum value of 2³¹-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 2³²-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 class to 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⁶³ and a maximum value of 2⁶³-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 class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.

不一定推荐这种方法。我只是让您了解该选项。

关于java - java中unsigned long的等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6742250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com