Get Bit operation || Bit manipulation in java language.

■  input->

import java.util.*;

public class Bits {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("enter a number=");

int n = sc.nextInt();

System.out.println("enter a position=");    

int pos=sc.nextInt();

int bitMask = 1<<pos;

    if((bitMask & n) == 0) {

      System.out.println("bit was zero");

    }

    else {

        System.out.println("bit was one");

    }

}

}

■ Output->

enter a number= 5

enter a position= 2

bit was one

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.