maxvalue+1

maxvalue+1

why?


System.out.println(Integer.MAX_VALUE );
System.out.println(Integer.MAX_VALUE + 1);

2147483647
-2147483648


max value should be 11111111......1111111111111 and then plus 1 is 10000000000000000000000000000000
and 1 is overflow so we got 0 - > how the computer handle this?
 
יש לך טעות בהנחה מספרים ב Java הם תמיד signed, כלומר הביט השמאלי הוא ביט הסימן

לכן

MAX_VALUE = 01111...1111
MAX_VALUE + 1 = 10000...0000

שזה הייצוג של המספר השלילי הגדול ביותר שניתן לייצג, בשיטת המשלים ל-2
 
למעלה