1

I was solving the following problem on Hacker rank. The program is supposed to print "Weird" onto the screen when N is odd. I checked the condition for N to be odd with the bitwise and(&) operator. But, I was getting this message from the compiler. I recently switched to java from c++. This operation works in C++. I wonder why it's not supported by java. enter image description here

public class Solution {
private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    int N = scanner.nextInt();
    if(N&1){
        System.out.println("Weird");
    }
    else if(N>=2&&N<=5)
    {
       System.out.println("Not Weird"); 
    }
    else if(N>=6&&N<=20){
        System.out.println("Weird");
    }
    else{
        System.out.println("Not Weird");
    }

    scanner.close();
}

}

3
  • 1
    FYI: Adding spaces makes the code more readable, e.g. else if (N >= 2 && N <= 5) Commented Feb 17, 2020 at 17:48
  • @Andreas whatareyoutalkingaboutwhatswrongwithit Commented Feb 17, 2020 at 17:50
  • @Michael At least CamelCase your ScriptioContinua; QUIDQUIDLATINEDICTUMSITALTUMSONATUR. Commented Feb 17, 2020 at 18:14

1 Answer 1

5

Java is not C, you can't coerce an int to a boolean. This

if(N&1){

should be

if ((N & 1) == 1) {
Sign up to request clarification or add additional context in comments.

2 Comments

Personally, I would bold, highlight, super-size, etc. the first 4 words, and make it blink in a whole rainbow of neon colors. But that's just me being old and cranky.
@Andreas Something like J͠a͠v͠a͠ ͠i͠s͠ ͠n͠o͠t͠ ͠C͠ .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.