You can use the modulus operator, but that can be slow. If it’s an integer, you can do:
if ( (x & 1) == 0 ) { even... } else { odd... }
This is because the low bit will always be set on an odd number.
More Related Contents:
- How starvation occurs in producer consumer problem
- Array Index out of Bound Merge Sort
- Sorting an ArrayList of objects using a custom sorting order
- Is a Java hashmap search really O(1)?
- Practical uses for AtomicInteger
- Moving objects and timers
- JPanel repaint issue
- Calculating and printing the nth prime number
- How is hashCode() calculated in Java
- Java: How to access methods from another class
- Regex exactly n OR m times
- How do I run a class in a WAR from the command line?
- Eclipse Installation on macOS
- Android: TextView automatically truncate and replace last 3 char of String
- Value Change Listener for JavaFX’s TextField
- Java 8 LocalDateTime deserialized using Gson
- What does `public static void main args` mean?
- Eclipse will not start and I haven’t changed anything
- Is there a no-duplicate List implementation out there?
- Why use returned instance after save() on Spring Data JPA Repository?
- Can I send an excel file and JSON body with a description of file in same REST API Response
- Spring: overriding one application.property from command line
- Java method overriding and variable shadowing
- Hibernate native query – char(3) column
- How to make a long time Full GC in Java manually
- Get minvalue of a Map(Key,Double)
- How to convert JSONArray to List with Gson?
- spring resttemplate url encoding
- Avoid duplicate submission of Struts 2 jsp page
- AES/CBC/PKCS5Padding vs AES/CBC/PKCS7Padding with 256 key size performance java