Skip to content

Code for LowestSetBitManipulation added #5574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.thealgorithms.bitmanipulation;

public final class LowestSetBit {

/**
* Method to isolate the lowest set bit of a given integer.
* @param n the integer input
* @return the isolated lowest set bit or 0 if n is 0
*/
public int isolateLowestSetBit(int n) {
return n & -n; // Works for both positive and negative
}

/**
* Method to clear the lowest set bit of a given integer.
* @param n the integer input
* @return the integer with the lowest set bit cleared
*/
public int clearLowestSetBit(int n) {
return n & (n - 1);
}
}