From 9d2cc533e873a83d7ab7e946cb670acf534751d9 Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Fri, 4 Oct 2024 13:14:55 +0530 Subject: [PATCH 1/8] LowestSetBit implementation and tests added --- .../bitmanipulation/LowestSetBit.java | 18 +++++++ .../bitmanipulation/LowestSetBitTest.java | 49 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java new file mode 100644 index 000000000000..78b80c3597ad --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java @@ -0,0 +1,18 @@ +package com.thealgorithms.bitmanipulation; + +public class LowestSetBit { + + /** + * Isolates the lowest set bit of the given number. + * For example, if n = 18 (binary: 10010), the result will be 2 (binary: 00010). + * + * @param n the number whose lowest set bit will be isolated + * @return the isolated lowest set bit of n + */ + public static int isolateLowestSetBit(int n) { + // Isolate the lowest set bit using n & -n + return n & -n; + } + +} + diff --git a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java new file mode 100644 index 000000000000..48a6810a6e7c --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java @@ -0,0 +1,49 @@ +package com.thealgorithms.bitmanipulation; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class LowestSetBitTest { + @Test + void testLowestSetBitWithPositiveNumber() { + // Test with a general positive number + assertEquals(2, LowestSetBit.isolateLowestSetBit(18)); // 18 in binary: 10010, lowest bit is 2 + } + + @Test + void testLowestSetBitWithZero() { + // Test with zero (edge case, no set bits) + assertEquals(0, LowestSetBit.isolateLowestSetBit(0)); // 0 has no set bits, result should be 0 + } + + @Test + void testLowestSetBitWithOne() { + // Test with number 1 (lowest set bit is 1 itself) + assertEquals(1, LowestSetBit.isolateLowestSetBit(1)); // 1 in binary: 0001, lowest bit is 1 + } + + @Test + void testLowestSetBitWithPowerOfTwo() { + // Test with a power of two (only one set bit) + assertEquals(16, LowestSetBit.isolateLowestSetBit(16)); // 16 in binary: 10000, lowest bit is 16 + } + + @Test + void testLowestSetBitWithAllBitsSet() { + // Test with a number with multiple set bits (like 7) + assertEquals(1, LowestSetBit.isolateLowestSetBit(7)); // 7 in binary: 111, lowest bit is 1 + } + + @Test + void testLowestSetBitWithNegativeNumber() { + // Test with a negative number (-1 in two's complement has all bits set) + assertEquals(1, LowestSetBit.isolateLowestSetBit(-1)); // -1 in two's complement is all 1s, lowest bit is 1 + } + + @Test + void testLowestSetBitWithLargeNumber() { + // Test with a large number + assertEquals(64, LowestSetBit.isolateLowestSetBit(448)); // 448 in binary: 111000000, lowest bit is 64 + } +} From d307e3161416407e9ef4fff712a0cd7025b578f8 Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Fri, 4 Oct 2024 14:49:03 +0530 Subject: [PATCH 2/8] clang formating done --- .../com/thealgorithms/bitmanipulation/LowestSetBit.java | 8 +++----- .../thealgorithms/bitmanipulation/LowestSetBitTest.java | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java index 78b80c3597ad..27f710b3aff6 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java @@ -3,8 +3,8 @@ public class LowestSetBit { /** - * Isolates the lowest set bit of the given number. - * For example, if n = 18 (binary: 10010), the result will be 2 (binary: 00010). + * Isolates the lowest set bit of the given number. For example, if n = 18 (binary: 10010), + * the result will be 2 (binary: 00010). * * @param n the number whose lowest set bit will be isolated * @return the isolated lowest set bit of n @@ -13,6 +13,4 @@ public static int isolateLowestSetBit(int n) { // Isolate the lowest set bit using n & -n return n & -n; } - -} - +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java index 48a6810a6e7c..f30fafb89e60 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java @@ -1,10 +1,10 @@ package com.thealgorithms.bitmanipulation; import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class LowestSetBitTest { + @Test void testLowestSetBitWithPositiveNumber() { // Test with a general positive number From 06f6fd4471d681df599aa01c16fca40a0a9887cd Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Fri, 4 Oct 2024 15:15:50 +0530 Subject: [PATCH 3/8] static imports are placed at the top. --- .../com/thealgorithms/bitmanipulation/LowestSetBitTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java index f30fafb89e60..5ff03d761678 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.bitmanipulation; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; public class LowestSetBitTest { @@ -46,4 +46,4 @@ void testLowestSetBitWithLargeNumber() { // Test with a large number assertEquals(64, LowestSetBit.isolateLowestSetBit(448)); // 448 in binary: 111000000, lowest bit is 64 } -} +} \ No newline at end of file From d78378206d1faae5ddce3dab862ad9c27cadba91 Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Fri, 4 Oct 2024 15:40:51 +0530 Subject: [PATCH 4/8] clang format changes --- .../java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java index 5ff03d761678..71ee1479afc1 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.bitmanipulation; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; public class LowestSetBitTest { From eff83ce6092dad31955a5b62a2a414cc9e14e113 Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Sat, 5 Oct 2024 00:03:45 +0530 Subject: [PATCH 5/8] clang format changes (removed comment) --- .../java/com/thealgorithms/bitmanipulation/LowestSetBit.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java index 27f710b3aff6..5cb79c339e37 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java @@ -10,7 +10,6 @@ public class LowestSetBit { * @return the isolated lowest set bit of n */ public static int isolateLowestSetBit(int n) { - // Isolate the lowest set bit using n & -n return n & -n; } } \ No newline at end of file From 4d237de442d5fb8e0d61838b454e7cf3688ba4fb Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Sat, 5 Oct 2024 00:55:47 +0530 Subject: [PATCH 6/8] clang format changes (added author and hide default constructor) --- .../bitmanipulation/LowestSetBit.java | 16 ++++++++++++---- .../bitmanipulation/LowestSetBitTest.java | 7 ++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java index 5cb79c339e37..82bb2b24f8ce 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java @@ -1,15 +1,23 @@ package com.thealgorithms.bitmanipulation; -public class LowestSetBit { +/** + * Lowest Set Bit + * @author Prayas Kumar (https://github.com/prayas7102) + */ +public class LowestSetBit { + // Private constructor to hide the default public one + private LowestSetBit() { + } /** - * Isolates the lowest set bit of the given number. For example, if n = 18 (binary: 10010), - * the result will be 2 (binary: 00010). + * Isolates the lowest set bit of the given number. For example, if n = 18 + * (binary: 10010), the result will be 2 (binary: 00010). * * @param n the number whose lowest set bit will be isolated * @return the isolated lowest set bit of n */ public static int isolateLowestSetBit(int n) { + // Isolate the lowest set bit using n & -n return n & -n; } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java index 71ee1479afc1..0a806e05ccad 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java @@ -4,6 +4,11 @@ import org.junit.jupiter.api.Test; +/** + * Test case for Lowest Set Bit + * @author Prayas Kumar (https://github.com/prayas7102) + */ + public class LowestSetBitTest { @Test @@ -47,4 +52,4 @@ void testLowestSetBitWithLargeNumber() { // Test with a large number assertEquals(64, LowestSetBit.isolateLowestSetBit(448)); // 448 in binary: 111000000, lowest bit is 64 } -} \ No newline at end of file +} From 611dfab2c190a8053234096f90f79ffd19b874de Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Sat, 5 Oct 2024 01:01:04 +0530 Subject: [PATCH 7/8] clang format changes (added final to LowestSetBit class) --- .../java/com/thealgorithms/bitmanipulation/LowestSetBit.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java index 82bb2b24f8ce..b8b873bf99cf 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java @@ -5,7 +5,7 @@ * @author Prayas Kumar (https://github.com/prayas7102) */ -public class LowestSetBit { +public final class LowestSetBit { // Private constructor to hide the default public one private LowestSetBit() { } From 8ef5e80bb3a78537376168cd9419f5b6bb6cea9b Mon Sep 17 00:00:00 2001 From: prayas7102 Date: Sat, 5 Oct 2024 10:58:26 +0530 Subject: [PATCH 8/8] clearLowestSetBit added with tests --- .../bitmanipulation/LowestSetBit.java | 11 +++++++ .../bitmanipulation/LowestSetBitTest.java | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java index b8b873bf99cf..127b6fa2c0b1 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java @@ -20,4 +20,15 @@ public static int isolateLowestSetBit(int n) { // Isolate the lowest set bit using n & -n return n & -n; } + /** + * Clears the lowest set bit of the given number. + * For example, if n = 18 (binary: 10010), the result will be 16 (binary: 10000). + * + * @param n the number whose lowest set bit will be cleared + * @return the number after clearing its lowest set bit + */ + public static int clearLowestSetBit(int n) { + // Clear the lowest set bit using n & (n - 1) + return n & (n - 1); + } } diff --git a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java index 0a806e05ccad..4c4d33640ad4 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java @@ -52,4 +52,35 @@ void testLowestSetBitWithLargeNumber() { // Test with a large number assertEquals(64, LowestSetBit.isolateLowestSetBit(448)); // 448 in binary: 111000000, lowest bit is 64 } + + @Test + void testClearLowestSetBitFor18() { + // n = 18 (binary: 10010), expected result = 16 (binary: 10000) + assertEquals(16, LowestSetBit.clearLowestSetBit(18)); + } + + @Test + void testClearLowestSetBitFor10() { + // n = 10 (binary: 1010), expected result = 8 (binary: 1000) + assertEquals(8, LowestSetBit.clearLowestSetBit(10)); + } + + @Test + void testClearLowestSetBitFor7() { + // n = 7 (binary: 0111), expected result = 6 (binary: 0110) + assertEquals(6, LowestSetBit.clearLowestSetBit(7)); + } + + @Test + void testClearLowestSetBitFor0() { + // n = 0 (binary: 0000), no set bits to clear, expected result = 0 + assertEquals(0, LowestSetBit.clearLowestSetBit(0)); + } + + @Test + void testClearLowestSetBitForNegativeNumber() { + // Test negative number to see how it behaves with two's complement + // n = -1 (binary: all 1s in two's complement), expected result = -2 (clearing lowest set bit) + assertEquals(-2, LowestSetBit.clearLowestSetBit(-1)); + } }