Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of unsigned long in java

Tags:

java

c

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

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

like image 656
Pramod Avatar asked Jul 19 '11 04:07

Pramod


3 Answers

Update – Java 8 can treat signed int & long as if unsigned

In Java, the primitive integer data types (byte, short, int, and long) are signed (positive or negative).

As of Java 8 both int and long can be treated explicitly as if they are unsigned. Officially a feature now, but kind of a hack nonetheless. Some may find it useful in certain limited circumstances. See the 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.

I am not necessarily recommending this approach. I’m merely making you aware of the option.

like image 59
Basil Bourque Avatar answered Nov 09 '22 13:11

Basil Bourque


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.

like image 26
LeleDumbo Avatar answered Nov 09 '22 12:11

LeleDumbo


Look over Java's Primitive Data Types. If you need something bigger than a long, try a BigInteger.

like image 29
Ryan Stewart Avatar answered Nov 09 '22 12:11

Ryan Stewart