• 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

MAX_VALUE

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Blue {
public static void main (String[] args) {
int i1 = (Integer.MAX_VALUE + 2);
int i2 = (Integer.MIN_VALUE + 1);
int i3 = (Integer.MIN_VALUE + 2);
int i4 = (Integer.MIN_VALUE + 3);
System.out.println((i1==i2)+","+(i1 ==i3)+","+(i1 ==i4));
}
}
can anyone explain the answer.
thnk u,
basanti
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer.MAX_VALUE + 1 will result in Integer.MIN_VALUE.
So Integer.MAX_VALUE + 2 will result in Integer.MIN_VALUE + 1.
So i1==i2 is true.
That's why the result is true,false,false.
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how did u arrive at the result. can u explain me?
basanti
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to study two's complement. It is the way Java represents Integral numbers.

Integer.MAX_VALUE = 0xEFFFFFFF
Integer.MIN_VALUE = 0x10000000
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you,
But is it calculated like this. pls correct me if am wrong.
max_value is represented in binary numbers and when u add 2 to it, convert 2 to binary numbers and then added to it?
where can i find the rules for binary addition.Are there rules for binary substraction and multiplication too?
thank again,
basanti
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose, I think you made a mistake about the MAX, MIN values of int
Max is 0x7FFFFFFF and Min is 0x80000000
that's because the MSB is used for the sign.
Binary addition rules are simple:
1 + 0 = 1
1 + 1 = 0 and carry 1
so for 0x7FFFFFFF + 1 it changes all the Fs to 0s and the 7 to 8 and you get MIN_VALUE. If you add another 1 then it becomes 0x80000001 which is (MIN_VALUE + 1)
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone,
basanti
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Alfred.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic