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 3 of 3

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,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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
    }

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