Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How does this print -1

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long lx=0x0FFFFL;
short ly=(short) lx;

System.out.println("The short value here is"+ly);
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Becuase the short value is FFFF and if you convert this Hex value using 2's complement notation it is -1.
 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would appreciate if you can explain this ind details I tried to do this but couldn't get -1.I used 15 for F value.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way to understand is that 2's complement is 1's complement + 1. Also the sign of the original number should be preserved. This will gives us :
FFFF = 1111111111111111
1's complement of this = 0000000000000000
Plus 1 will make : 0000000000000001
The original number was negative (1 in the MSB position)
So the answer is -1.
Thanks, Sudd
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This particular area is one that I could use more practice with.
I'd like some type of textbook excercises for this with a brief tutorial.
anyone?
 
Sudd Ghosh
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This subject can be a little tricky if one is not sure of the rules. But the rules are pretty straightforward.
I think one of the Dietel's "How to program.." book used to have an appendix on Number Systems. I found this section very useful. Or, if you have access to some Digital Electronics book, those will definitely cover the complement arithmetics. (I learnt from one of those in my college years)
Sudd
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic