• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

~ operator

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to find ~.

int x = ~4.
The answer is x=-5. How to find it.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiply by -1 and subtract one. When calculating the negative of an int, internally you have a 2's complement representation of the positive number - you flip all the bits and then add 1. So, when you flip all the bits, you basically make a negative (flip all bits, add 1) and then subtract the extra 1!
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i could understand up to some extent, can you please interpret this last line,

suppose ~4

0100

1's complement : 1011

2's complement : 1011
0001
----------------
1100 == but it is 12 (not -5)...
how could i get - 5 from 1100. mean how to subtract 1
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the MSB is 1, it will not be interpreted as 12, but as -4 (since you made the 2's complement of 4). If we follow your reasoning that it is 12, it is still wrong because the int 4 is represented not as 0100, but as 00000000000000000000000000000100, so the 2's complement would be 11111111111111111111111111111100. when you subtract 1, (or add -1 which is 11111111111111111111111111111111) you get -5, or 11111111111111111111111111111011. (which is basically just all the bits of 4 flipped)

I thought you were just looking for a way to determine what ~x will be, given x. It is simply -x - 1.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks man. Great
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic