计算一个十进制数字n,它对应的二进制第k位的值
Given a number n and k (1 <= k <= 32), find the value of k-th bit in the binary representation of n. Bits are numbered from right (Least Significant Bit) to left (Most Significant Bit).
Examples :
Input : n = 13, k = 2 Output : 0 Explanation: Binary representation of 13 is 1101. Second bit from right is 0. Input : n = 14, k = 3 Output : 1 Explanation: Binary representation of 14 is 1110. Third bit from right is 1.
import java.io.*; class GFG { static void printKthBit(long n, long k) { System.out.println( ((n & (1 << (k - 1))) >> (k - 1))); } // Driver Code public static void main(String[] args) { long n = 13, k = 2; // Function Call printKthBit(n, k); } }