Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help me to figure out how code working

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help me to figure out how code working

    Hello everyone. Please tell me how working function
    number = scanner.nextInt();
            if ((number & 0) == 1)`

    Full code

    package com.company;
     
    import java.util.Scanner;
     
    public class CheckParity {
        public static void main(String[] args) {
            int number;
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter number: ");
            number = scanner.nextInt();
            if ((number & 0) == 1) {
                System.out.println("Even number");
            } else {
                System.out.println("The number is not even");
            }
        }
    }


    --- Update ---

    The most of I can't understand if ((number & 0) == 1)

    Why we using number and &?

    & its and. So why we do that?

    Why we comparing number and 0? And why after priority () we use == 1?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help me to figure out how code working

    Does that code work correctly?

    Do you know of the results for an AND operation?
    1 AND 1 = 1
    1 AND 0 = 0
    0 AND 1 = 0
    0 AND 0 = 0

    ANDing anything with 0 always gives 0


    Odd integer values have a low order bit that is 1
    Even integer values have a low order bit that is 0
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Jexly (January 12th, 2022)

  4. #3
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Help me to figure out how code working

    Quote Originally Posted by Jexly View Post
    Why we using number and &?
    i don't think this code is correct. And regardless, using bitwise operators (&, | etc. ) only really makes sense if you are actually interested in individual bits.
    Seems like the assignment here is to find out if a number is odd or even. So the proper test would be

    if ( number % 2 == 0 )
    {
         even
    }
    else
    {
         odd
    }

  5. #4
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Post Re: Help me to figure out how code working

    Certainly! Let's break down the code:

    ```java
    number = scanner.nextInt();
    if ((number & 0) == 1)
    ```

    1. `number = scanner.nextInt();`: This line reads an integer input from the user using the Scanner class and assigns it to the variable 'number'.

    2. `if ((number & 0) == 1)`: This line checks if the bitwise AND operation between 'number' and '0' equals 1.

    - The `&` operator performs a bitwise AND operation, meaning it compares each bit of the operands and returns 1 only if both bits are 1.

    - In this case, 'number' is compared with '0', which results in 0 for any integer since bitwise AND with 0 results in 0.

    - So, effectively, `(number & 0)` always equals 0, regardless of the value of 'number'.

    - Consequently, the condition `(number & 0) == 1` is always false, because `(number & 0)` is always 0, and 0 is not equal to 1.

    In essence, the condition if ((number & 0) == 1) will always result in the "The number is not even" message being printed, regardless of the value of 'number'. Therefore, it seems there might be a misunderstanding or an error in the logic of the code. If you're seeking help with Java assignment or need help understanding bitwise operations and logical conditions better, there are various online resources and communities where you can find support and guidance, such as ProgrammingHomeworkHelp.com.

Similar Threads

  1. [SOLVED] I cant figure out why this code isn't doing what i want.
    By NyitStudent07 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 25th, 2019, 08:23 AM
  2. Replies: 6
    Last Post: September 6th, 2014, 10:29 AM
  3. [SOLVED] I can't figure out why this isn't working!
    By samjoyboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:04 PM
  4. Cannot figure out how to code this...
    By compsci21 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 20th, 2012, 11:56 PM
  5. my code isn't working an i can't figure out why
    By jack13580 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 12:21 PM