We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2338d99 commit 95b810eCopy full SHA for 95b810e
1 file changed
Bit Manipulation/IsNthBitSet.java
@@ -25,9 +25,17 @@ public static void main(String[] args) {
25
}
26
27
public static void checkNthBit(int a, int n) {
28
- if ((a & (1 << (n - 1))) != 0) {
+ //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) {
34
System.out.println("Bit is set at position " + n);
- } else {
35
+ }
36
+ //If nth bit is unset in 'a' then bitwise AND with mask
37
+ //will give zero result
38
+ else {
39
System.out.println("Bit is unset at position " + n);
40
41
0 commit comments