as we know Java doesn't support unsigned operations because it considers the last bit as a sign bit for all integers. I know that one way is to use a greater size defined for the numbers involving in the operations. for example if you want to have 32-bit operations, you can do them by long which is 64-bit, then consider the bottom 32 bits as the result. but the point is that, it takes twofold memory. I read something about using the class integer but i didn't understand it. Do you have any idea to do unsigned operations with the least memory used? thanks!
-
2You read something but didn't understand it? What did you read? What didn't you understand? Maybe someone can clear up the bit you got stuck on – khelwood Jul 03 '17 at 12:11
-
1You are worrying about something completly irrelevant. If you need to programm something where the memory restrictions are so tight that you have to worry about wether you use signed or unsigned numbers, then java is most likely not the right tool for that job anyway. – OH GOD SPIDERS Jul 03 '17 at 12:11
-
Did you look [here](https://stackoverflow.com/q/25556017)? – Dawood ibn Kareem Jul 03 '17 at 12:14
-
no, i must use Java :-) and i am just looking for the best way. as i explained i know one way that i told you, but i don't want to use that much memory for it. Do you know any specific function for example in the integer class, which can be used to do unsigned operations? – A.Aalipour Jul 03 '17 at 12:18
-
ok, thanks , got it! ;-) – A.Aalipour Jul 03 '17 at 12:29
2 Answers
"signed" and "unsigned" is about the interpretation of bit patterns as numeric values. And most (all?) modern CPUs (and the JVM) use Two's Complement representation for signed numbers, having the interesting property that for addition and subtraction, the bit pattern operations are the same as for unsigned numbers.
E.g. In 16-bit, the pattern 0xfff0 is 65520 when interpreted as an unsigned, and -16 as signed number. Subtracting 16 (0x0010) from that number gives 0xffe0, which represents the correct result both for the signed (being -32) as well as the unsigned case (65504).
So, depending on the operations you are doing on your numbers, there are cases where you can simply ignore the signed/unsigned question - just be careful with input and output.
Regarding the relevance of using the shorter representation, only you can tell. And Java can be quite fast when doing numerics. I remember a coding contest quite some years ago where even Java 1.4 outperformed Microsoft C (both with default settings - with optimization switches C was a bit faster).

- 6,990
- 1
- 13
- 7
As we know Java doesn't support unsigned operations because it considers the last bit as a sign bit for all integers.
This is incorrect.
While Java does not support 32 and 64 bit unsigned integer types, it DOES support unsigned integer operations; see the javadoc for the Integer
and Long
types, and look for the xxxUnsigned
methods.
Note that you won't see methods for add, subtract or multiply because they would return the same representations as the primitive operators do.
In short, you can use the 32 bit signed int
type to do 32 bit unsigned arithmetic and comparison operations.
For more information:

- 698,415
- 94
- 811
- 1,216