Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

How would I determine whether a number is even or odd?

1 Answer

0 votes
by (13.1k points)

I know everyone prefers using modulus, but that can be slow. If it is an integer, you can do this:

if ( (x & 1) == 0 ) { even... } else { odd... }

This is because the low bit will always be set on an odd number.

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...