Help coderanch get a
new server
by contributing to the fundraiser
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

# print same values?

Ranch Hand
Posts: 252
• Number of slices to send:
Optional 'thank-you' note:

why does the o/p prints same -ve values?? instead of one +ve and other -ve

Ranch Hand
Posts: 340
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Thomas, when you flip the bits you get ones-complement. Twos complement is the ones-complement + 1.

Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
That is right

author
Posts: 23956
142
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Refer Java Language Specfication [JLS]. It has good explanation and examples for this kind of scenario.

Mani vannan
Ranch Hand
Posts: 185
• Number of slices to send:
Optional 'thank-you' note:
Refer Java Language Specfication [JLS]. It has good explanation and examples for this kind of scenario.

Roel De Nijs
Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:
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

 Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser