• 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

~10 = -11?

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

can any one explain me why 10 complement is -11 in java and not 5

thank you
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

siddharth sekhar wrote:
can any one explain me why 10 complement is -11 in java and not 5



Can you explain to us why the complement of 10 should be 5? I don't see how you got to that assumption.


Anyway to answer your question. Integers (whole numbers) are stored in Twos complement format. And a complement (meaning ones complement) of a number is simply reversing the bits. And when you reverse the bits of 10 in twos complement, that is what you get.

http://en.wikipedia.org/wiki/Two's_complement

Henry
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What the ~ operator does is to return the one's complement of the number. It only works for integers. Two's complement is defined by subtraction, but there are other formulae which give the same results.
If you work out the two's complement of i in n bits, it is 2ⁿ − i: for 10 as an int that is
0b1_0000_0000_0000_0000_0000_0000_0000_0000 −
    0b0000_0000_0000_0000_0000_0000_0000_1010
    0b1111_1111_1111_1111_1111_1111_1111_0110

The one's complement is obtained by inverting every bit. It can be calculated from 2ⁿ − 1 − i: for 10 as an int that is
0b1111_1111_1111_1111_1111_1111_1111_1111 −
0b0000_0000_0000_0000_0000_0000_0000_1010
0b1111_1111_1111_1111_1111_1111_1111_0101 which means −11 QED

You can try it for any number: you will see there is an extra − 1 in the formula: so when you take the one's complement of 10 you get −11.
Notice that the formulae for complement arithmetic give different results depending how many bits you have, so you should always say, “Two's complement of 12345 in 32 bits” or similar. There is also ten's complement but you don't use that in computing.
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic