Carbs0126 / AvatarImageView

an AvatarImageView on Android platform which can display circle text or circle image
474 stars 86 forks source link

Prevent negative absolute value of signed 32-bit hashcode #2

Closed vpop closed 8 years ago

vpop commented 8 years ago

If the hashCode() method returns Integer.MIN_VALUE, the return of the Math.abs call will be negative (Integer.MIN_VALUE). This produces a negative modulo result, hence a negative array index for COLORS.

More details on this corner case here, slide 4 (called the histogram mystery).

Carbs0126 commented 8 years ago

Thank you for your consideration, i didnot get the point that the Math.abs(Integer.MIN_VALUE) equals Integer.MIN_VALUE, not a positive number, and thank you!

vpop commented 8 years ago

Int primitive goes from Integer.MIN_VALUE, or -231 (−2,147,483,648) to Integer.MAX_VALUE, or 231-1 (2,147,483,647). There is no positive equivalent for the −2,147,483,648, so Math.abs(−2,147,483,648) returns the same negative value.

Carbs0126 commented 8 years ago

It's all now clear after your explanation, thank you