Java Bitwise AND (&) to check a given number is odd or even

 An even number is an integer that is divisible by 2, i.e., when you divide the integer by 2, the remainder will be 0.
Examples : -6, 0, 4, 100 .An odd number is an integer that is not divisible by 2. i.e. when you divide the integer by 2, the remainder will be 1.
Examples are -3, 1, 7, and 101.There are many ways to find the given integer is odd or even. Let us write the java program using the following operators
1. Modulus (Remainder) (%)
2. Bitwise AND Operator (&)
3. Division (/)
1. Using Remainder (%) operator :

The Remainder ( mod) operator gives the remainder of a division. When you divide an integer by 2, if the remainder is 0, then the given integer is even; if the remainder is 1 , then the given integer is odd.The simple logic can be written in java as given below.

if (number % 2==0) sop(“even”) else sop (“odd”);

Somebody may ask you a question, whether the number 192322423 is odd or even?. You will answer “odd” in a second without even thinking, but just seeing the last digit of the number. If the last digit is odd (i.e. 1, 3, 5, 7, or 9), then the given number is odd otherwise the given number is even. You can write the following line of code in java.

int ld=number%10;

if( ld ==1 || ld==3 || ld==5 || ld==7 || ld==9) sop(“odd”) else sop(“even”);

2. Using Bitwise AND Operator (&) :

AND (&) operator returns a one in each bit position for which the corresponding bits of both operands are ones; otherwise returns zero. The idea to find odd or even using the last digit of a number will also work for the numbers in binary format. The binary number is odd if its last digit is 1 and even if its last digit is 0.

 

To get the last digit of the binary number, do AND (&) operation of the number with 1.

number & 1 will give you the last digit of the binary number.

For example , 5 is odd, let us find out using bitwise AND(&) operation

0000 0101 & 0000 0001 = 0000 0001, as the last digit of the binary number is 1 , then it is odd.

Number 6 is even , let us see using bitwise AND (&) operation

0000 0110 & 0000 0001 = 0000 0000, as the last digit is 0 , then it is even.

The line of code in java using Bitwise AND(&) operation is
if( (number&1) == 0) sop(“even”) else sop (“odd”);

3. Using division (/) operator :

When you divide an integer by 2, if you get the result as “integer” number, then it is even, otherwise the given number is odd. You may write the logic in java as given below.if((number /2)==((double)number)/2) sop(“even”) else sop (“odd”)

Note : sop is System.out.println. number is the input integer.

The complete java program to find the given number is odd or even is given below.


package net.javaonline;

public class OddEven{

      public static void main(String args[]){             

      //Give your input as Command Line Arguments.

      if (args.length!=1)
    		 {
    			System.out.println("Incomplete Input");
    			return;
    		}

        int number = Integer.parseInt(args[0]);

       System.out.printf("\nChecking  Odd or  Even number using  Remainder  operators\n");

//Logic : 1

        if((number %2)==0){

            System.out.printf( number + " is even %n"); //%n for new line

        } else{

            System.out.printf( number + " is odd %n");

        }         

//Logic  : 2 

   int ld=number%10;

        if(ld==1 || ld==3 || ld==5 || ld==7 || ld==9 ){

                System.out.printf( number + " is odd %n");

        } else{

            System.out.printf( number + " is even %n");

        }         

        System.out.printf("\nChecking  Odd or  Even number using  Bitwise AND operator \n");

   //Logic : 3   

        if( (number&1) == 0){

             System.out.printf( number + " is even %n");

        } else{

            System.out.printf( number + " is odd %n");

        }         

   System.out.printf("\nChecking  Odd or  Even number using  Divide operator \n");

//Logic : 4

       if((number /2)==((double)number)/2){

            System.out.printf( number + " is even %n");

        } else{

            System.out.printf( number + " is odd %n");

        }         

    }

}

odd evensd

 

Run it Online at  OddEven.java

You may also like

Leave a Reply

Be the First to Comment!