Checks if the bit at position pos of an integer n is set. - Java java.lang

Java examples for java.lang:byte Array to int

Description

Checks if the bit at position pos of an integer n is set.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int n = 2;
        int pos = 2;
        System.out.println(isBitSet(n, pos));
    }/*from ww w .j  a v a 2 s .  c  o  m*/

    /**
     * Checks if the bit at position <i>pos</i> of an integer <i>n</i> is set.
     * 
     * @param n
     *            The integer to check.
     * @param pos
     *            The bit position of interest.
     * @return true if the is set, false otherwise.
     */
    public static boolean isBitSet(int n, int pos) {
        return ((n >>> pos) & 1) != 0;
    }
}

Related Tutorials