Skip to content

Commit 890223b

Browse files
committed
unset rightmost bit
1 parent 5160737 commit 890223b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
- [x] [Reverse bits](https://leetcode.com/problems/reverse-bits/)
5656
- [x] [Bit Hacks — Part 1 (Basic)](http://www.techiedelight.com/bit-hacks-part-1-basic/)
5757
- [x] [Bit Hacks — Part 2 (Playing with k’th bit)](http://www.techiedelight.com/bit-hacks-part-2-playing-kth-bit/)
58+
- [x] [Bit Hacks — Part 3 (Playing with rightmost set bit of a number)](http://www.techiedelight.com/bit-hacks-part-3-playing-rightmost-set-bit-number/)
59+
5860

5961
## Algorithms
6062

@@ -172,7 +174,6 @@
172174
## To Do
173175

174176
- Bits Manipulation
175-
- [ ] [Bit Hacks — Part 3 (Playing with rightmost set bit of a number)](http://www.techiedelight.com/bit-hacks-part-3-playing-rightmost-set-bit-number/)
176177
- [ ] [Bit Hacks — Part 4 (Playing with letters of English alphabet)](http://www.techiedelight.com/bit-hacks-part-4-playing-letters-english-alphabet/)
177178
- [ ] [Bit Hacks — Part 5 (Find absolute value of an integer without branching)](http://www.techiedelight.com/bit-hacks-part-5-find-absolute-value-integer-without-branching/)
178179
- [ ] [Bit Hacks — Part 6 (Random Problems)](http://www.techiedelight.com/bit-hacks-part-6-random-problems/)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package binary.rightmostBit;
2+
3+
import utils.InputReader;
4+
5+
// The expression n & (n-1) will turn off the rightmost set bit of a number n
6+
public class Unsetter {
7+
public static void main(String[] args) {
8+
System.out.print("Input a number: ");
9+
int x = InputReader.readInt();
10+
System.out.print("Unsetting the rightmost bit: " + unset(x));
11+
}
12+
13+
public static int unset(int x) {
14+
return x & (x-1);
15+
}
16+
}

0 commit comments

Comments
 (0)