Skip to content

Commit 95b810e

Browse files
committed
IsNthBitSet code and comments added
1 parent 2338d99 commit 95b810e

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

Bit Manipulation/IsNthBitSet.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ public static void main(String[] args) {
2525
}
2626

2727
public static void checkNthBit(int a, int n) {
28-
if ((a & (1 << (n - 1))) != 0) {
28+
//Create a mask that has only nth bit set
29+
int mask = 1 << (n - 1);
30+
31+
//If nth bit is set in 'a' then bitwise AND with mask
32+
//will give non-zero result
33+
if ((a & mask) != 0) {
2934
System.out.println("Bit is set at position " + n);
30-
} else {
35+
}
36+
//If nth bit is unset in 'a' then bitwise AND with mask
37+
//will give zero result
38+
else {
3139
System.out.println("Bit is unset at position " + n);
3240
}
3341
}

0 commit comments

Comments
 (0)