• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

signed to unsigned integer

 
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lilou Laure wrote:. . . the integer is interpreted as a signed number, its not happening correctly. . . . .

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 primitive 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.

Campbell'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)

Notice that 2222222222 is interpreted in two's complement. If you tried Integer.parseInt("2222222222") you would suffer a number format exception.

So, ~1 has always been −2 and it still is −2.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !  
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.  
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Lilou Laure wrote:. . . the integer is interpreted as a signed number, its not happening correctly. . . . .

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 primitive 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.



Oh okay I get it, thanks Campbelle Ritchie  
 
Rob Spoor
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic