public boolean checkIfOdd(int num) {
return num % 2 == 1;
}
Again, contrary to the instinctive yes answer, this won't work half of the time. Why? Because half of the integers are negative, half are not. This method will falsely identify negative numbers as an even number.
The info of this oddity is courtesy of the Java Puzzlers book.
richard@work
1 comment:
public boolean checkIfOdd(int num) {
return num & 0x1 != 0;
}
Post a Comment