int the above code i am trying to flip the bits of an integer. However since the integer is interpreted as a signed number, its not happening correctly. I have tried alternative solutions like masking using & , or using the XOR operation to flip bits , etc. but I want to learn the useage of this new parseUnsignedInt method in Java 8 .

So, can someone tell me how to use this method and get my desired result above ? I want to use this method to interpret my integer as an unsigned number and flip its bits accordingly as though it were an unsigned integer.

That is correct. It is the String input that has to be unsigned. Just because there is a method for parsing Strings as unsigned, that doesn't mean there has been a change in the structure of the primitiveLilou Laure wrote:. . . the integer is interpreted as a signed number, its not happening correctly. . . . .

`int`datatype, which is as always two's complement. Have you read the details of the Integer#parseUnsignedInt() method? It returns an ordinary

`int`. Maybe Java® is wrong not to have unsigned numbers, but it doesn't. Link explaining why unsigned numbers might be unhelpful. So the unsigned method doesn't accept negative arguments.

Notice that 2222222222 is interpreted in two's complement. If you triedCampbell's Computer wrote:java UnsignedIntDemo 0 +0 1 +1 1234567890 2222222222 -1234567890

0

0

1

1

1234567890

-2072745074

Exception in thread "main" java.lang.NumberFormatException: Illegal leading minus sign on unsigned string -1234567890.

at java.base/java.lang.Integer.parseUnsignedInt(Integer.java:827)

at java.base/java.lang.Integer.parseUnsignedInt(Integer.java:928)

at UnsignedIntDemo.main(UnsignedIntDemo.java:7)

`Integer.parseInt("2222222222")`you would suffer a number format exception.

So, ~1 has always been −2 and it still is −2.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Campbell Ritchie wrote:

That is correct. It is the String input that has to be unsigned. Just because there is a method for parsing Strings as unsigned, that doesn't mean there has been a change in the structure of the primitiveLilou Laure wrote:. . . the integer is interpreted as a signed number, its not happening correctly. . . . .

intdatatype, which is as always two's complement. Have you read the details of the Integer#parseUnsignedInt() method? It returns an ordinaryint. Maybe Java® is wrong not to have unsigned numbers, but it doesn't. Link explaining why unsigned numbers might be unhelpful. So the unsigned method doesn't accept negative arguments.

Oh okay I get it, thanks Campbelle Ritchie

Lilou Laure wrote:Yes, this is exactly what I wanted to know ! Thanks Rob Spoor !

You're welcome

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions