Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output the absolute value of an Unsigned integer in java [duplicate]

I want to assign 4294967295 to a variable (2^32-1) It is obvious that I can't do that with Integer, and can do it with Long.

However, I noted that Java 8 offers Unsigned Integers (at least some methods).

Does any one know what the method, Integer.parseUnsignedInt() does? When I input "4294967295" to that, and print the variable, it gives the output as -1 (-2 for 4294967294, -3 for 4294967293 and so on...)

Is there a way that I can still have 4294967295 in a variable?

Am I missing something here?

a=Integer.parseUnsignedInt("4294967295");
System.out.println(a);

This gives the output as -1 but I expected 4294967295.

like image 567
Pubudu Dodangoda Avatar asked Oct 15 '14 11:10

Pubudu Dodangoda


People also ask

Does ABS return an int?

Threadsafe: Yes. The abs() function returns the absolute value of an integer argument n .

Is Double signed or unsigned in Java?

Java only supports signed types (except char ) because it was assumed that one type was simpler for beginners to understand than having two types for each size. In C it was perceived to be a source of error so support for unsigned types was not included.

How do you treat unsigned integer in Java?

For most purposes, all integers in Java are signed. However, you can treat a signed integer as unsigned in one specific case: you can shift right without sign extending by using >>> operator instead of >> .

How do you declare an unsigned integer in Java?

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.


2 Answers

You can view that integer as unsigned int by calling toUnsignedString():

int uint = Integer.parseUnsignedInt("4294967295");
System.out.println(Integer.toUnsignedString(uint));

You can also call some other methods that interpret that int as unsigned.

For example :

long l = Integer.toUnsignedLong(uint);
System.out.println(l); // will print 4294967295

int x = Integer.parseUnsignedInt("4294967295");
int y = 5;
int cmp1 = Integer.compareUnsigned(x,y); // interprets x as 4294967295 (x>y) 
int cmp2 = Integer.compare(x,y); // interprets x as -1 (x<y) 
like image 103
Eran Avatar answered Oct 19 '22 10:10

Eran


As far as I understand https://blogs.oracle.com/darcy/entry/unsigned_api, the unsigned support is not done by introducing a new type. The values are still stored in (signed) int variables but they provide methods to interpret the value as unsigned:

To avoid dealing with the overhead of boxed values and to allow reuse of the built-in arithmetic operators, the unsigned API support does not introduce new types like UnsignedInt with instance methods to perform addition, subtraction, etc. However, that lack of separate Java-level unsigned types does mean a programmer can accidentally improperly mix signed and unsigned values.

You used the "unsigned" version of parse, but you don't show which method you use to "print the variable". Probably you picked the (default) signed one.

like image 43
chiccodoro Avatar answered Oct 19 '22 10:10

chiccodoro