Java Program to Check if a given Bit Position is Set to One or not

In this Java program, we will have a glance at how to check if a given Bit position is set to one or not.

Check if a given Bit Position is Set to One or not in Java

import java.io.*;
public class GFG {
static int bitAtGivenPosSetOrUnset( int n, int k)
{ 
   int new_num = n >> (k - 1);
   return (new_num & 1);
}
   public static void main (String[] args)
    {
         int n = 10, k = 2;
    if (bitAtGivenPosSetOrUnset(n, k)==1)
        System.out.println("Set");
    else
        System.out.println("Unset");
    }
}

Output:

Set

Leave a Comment

Your email address will not be published. Required fields are marked *