+ 7

Java Problem

In Java, why (num&1)==1 throwing error instead of giving answer 1?

27th Jun 2018, 5:01 AM
Ajay Agrawal
Ajay Agrawal - avatar
6 odpowiedzi
+ 2
try to do like this public class EvenOddBitwise { public static void main(String[] args) { int number = 65; if((number & 1) == 1) { System.out.println(number + " is Odd."); } else { System.out.println(number + " is Even."); } } }
27th Jun 2018, 5:16 AM
MsJ
MsJ - avatar
+ 20
Every odd number have 1 at the end of its binary representation! public static boolean isEven(int num) { return (num & 1) == 0; } //  (number % 2) or (number & 1)
27th Jun 2018, 10:23 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 20
Bitwise operators Java Bitwise AND "&" --> returns true if and only if both arguments are true! 1 & 1 = 1 0 & 0 = 0 0 & 1 = 0
27th Jun 2018, 10:29 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 7
Thankyou friends for help
27th Jun 2018, 9:11 AM
Ajay Agrawal
Ajay Agrawal - avatar
+ 6
Hello, Ajay Agrawal ! Please, read the lessons from SoloLearn so that the problems do not arise any more. https://www.sololearn.com/learn/Java/2144/ https://www.sololearn.com/learn/Java/2143/
27th Jun 2018, 5:17 AM
Alexander Sokolov
Alexander Sokolov - avatar
+ 2
you can use (num%2) to find Even/Odd It would be better if you put your code in the question.
27th Jun 2018, 5:42 AM
Daniel (kabura)
Daniel (kabura) - avatar