From Jashua Bloch's "Java Puzzlers" book.
The reason for this would be clear once you read about modulus operator.
// Wrong way to check the oddity public boolean isOdd(int num){ return (num % 2 == 1) // Broken- for all -ve odd numbers }
The reason for this would be clear once you read about modulus operator.
// Right Solution public boolean isOdd(int num){ return (num % 2 != 0) } // Much better and Faster public boolean isOdd(int num){ return (num & 1) }
No comments:
Post a Comment