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.

What result do you want to get?

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

Rob Spoor wrote:If you want to read 1, flip its bits and expect a positive number, you probably should move the conversion to unsigned to later in the process:

Yes, this is exactly what I wanted to know ! Thanks Rob Spoor !

Pierre-Yves Saumont wrote:What you get is the correct result given the fact that negative numbers are represented in Two's complement:

What result do you want to get?

I wanted the result as posted by Rob Spoor. But thanks for your response as well.

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