• 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
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

print same values?

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


why does the o/p prints same -ve values?? instead of one +ve and other -ve
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

-Integer.MIN_VALUE= Integer.MAX_VALUE+1
and
Integer.MAX_VLAUE+x= Integer.MIN_VALUE-(x-1)


so
-Integer.MIN_VALUE= Integer.MAX_VALUE+1
-(-2147483648)=2147483648 (which is 1 greater then MAX_VALUE)

hence result is :

Integer.MAX_VLAUE+x= Integer.MIN_VALUE-(x-1)
-2147483648-(1-1)
=> -2147483648-0
=> -2147483648

Hence both the values are same.

I know the way I Have expalined is bit difficult...
But this is the way it works.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here a short program to illustrate what Sandeep is saying.





results of running program:

MIN_VALUE FOR int a = 10000000000000000000000000000000 or -2147483648
Two Complements of a =1111111111111111111111111111111 or 2147483647
Two Complements + 1 a = 10000000000000000000000000000000 or -2147483648
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas, when you flip the bits you get ones-complement. Twos complement is the ones-complement + 1.
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now 2 complements is mentioned, got a question about it:

all numeric types in java are signed, so the leftmost bit is indicating the sign (1 = negative, 0 is positive). so far so good. but can someone explain the two's complement method to calculate the value of a bit representation

1100 0010 = ?
0100 0010 = ?

and whats the bit representation of 20 and -20 ?

Thanks!
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is right
 
author
Posts: 23959
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

Originally posted by Roel De Nijs:
but can someone explain the two's complement method to calculate the value of a bit representation



As already mentioned, a twos complement of a number is to flip all the bits and add one. The question you might be asking is why would anyone design such a silly way to represent negative numbers... believe it or not, it is because it makes the math easier !

To add and subtract numbers, with twos complement, you just ignore the negative bit, treat everything as unsigned numbers, and everything works... for example (16 bits only) ...



If you add one to negative one it will roll over and get you a zero -- and this will work for all cases.

Henry
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer Java Language Specfication [JLS]. It has good explanation and examples for this kind of scenario.
 
Mani vannan
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer Java Language Specfication [JLS]. It has good explanation and examples for this kind of scenario.
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if i get it right:
- to know the bit representation of a negative number, you take the bit representation for the positive number, flip all bits and add 1
- if you have the bit representation of a negative number and want to know the positive one, just do the same: flip all bits and add 1
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic