Skip to content

Commit 323b5b8

Browse files
authored
Merge branch 'master' into matrix_graphs_add_tests
2 parents 600fed9 + c657138 commit 323b5b8

File tree

168 files changed

+8815
-1769
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+8815
-1769
lines changed

DIRECTORY.md

+79-2
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to find the first differing bit
5+
* between two integers.
6+
*
7+
* Example:
8+
* x = 10 (1010 in binary)
9+
* y = 12 (1100 in binary)
10+
* The first differing bit is at index 1 (0-based)
11+
* So, the output will be 1
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class FirstDifferentBit {
16+
private FirstDifferentBit() {
17+
}
18+
19+
/**
20+
* Identifies the index of the first differing bit between two integers.
21+
* Steps:
22+
* 1. XOR the two integers to get the differing bits
23+
* 2. Find the index of the first set bit in XOR result
24+
*
25+
* @param x the first integer
26+
* @param y the second integer
27+
* @return the index of the first differing bit (0-based)
28+
*/
29+
public static int firstDifferentBit(int x, int y) {
30+
int diff = x ^ y;
31+
return Integer.numberOfTrailingZeros(diff);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* This class provides a method to generate all subsets (power set)
8+
* of a given set using bit manipulation.
9+
*
10+
* @author Hardvan
11+
*/
12+
public final class GenerateSubsets {
13+
private GenerateSubsets() {
14+
}
15+
16+
/**
17+
* Generates all subsets of a given set using bit manipulation.
18+
* Steps:
19+
* 1. Iterate over all numbers from 0 to 2^n - 1.
20+
* 2. For each number, iterate over all bits from 0 to n - 1.
21+
* 3. If the i-th bit of the number is set, add the i-th element of the set to the current subset.
22+
* 4. Add the current subset to the list of subsets.
23+
* 5. Return the list of subsets.
24+
*
25+
* @param set the input set of integers
26+
* @return a list of all subsets represented as lists of integers
27+
*/
28+
public static List<List<Integer>> generateSubsets(int[] set) {
29+
int n = set.length;
30+
List<List<Integer>> subsets = new ArrayList<>();
31+
32+
for (int mask = 0; mask < (1 << n); mask++) {
33+
List<Integer> subset = new ArrayList<>();
34+
for (int i = 0; i < n; i++) {
35+
if ((mask & (1 << i)) != 0) {
36+
subset.add(set[i]);
37+
}
38+
}
39+
subsets.add(subset);
40+
}
41+
42+
return subsets;
43+
}
44+
}

src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Is number power of 2
4+
* Utility class for checking if a number is a power of two.
5+
* A power of two is a number that can be expressed as 2^n where n is a non-negative integer.
6+
* This class provides a method to determine if a given integer is a power of two using bit manipulation.
7+
*
58
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
69
*/
7-
810
public final class IsPowerTwo {
911
private IsPowerTwo() {
1012
}
13+
14+
/**
15+
* Checks if the given integer is a power of two.
16+
*
17+
* A number is considered a power of two if it is greater than zero and
18+
* has exactly one '1' bit in its binary representation. This method
19+
* uses the property that for any power of two (n), the expression
20+
* (n & (n - 1)) will be zero.
21+
*
22+
* @param number the integer to check
23+
* @return true if the number is a power of two, false otherwise
24+
*/
1125
public static boolean isPowerTwo(int number) {
1226
if (number <= 0) {
1327
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to compute the remainder
5+
* of a number when divided by a power of two (2^n)
6+
* without using division or modulo operations.
7+
*
8+
* @author Hardvan
9+
*/
10+
public final class ModuloPowerOfTwo {
11+
private ModuloPowerOfTwo() {
12+
}
13+
14+
/**
15+
* Computes the remainder of a given integer when divided by 2^n.
16+
*
17+
* @param x the input number
18+
* @param n the exponent (power of two)
19+
* @return the remainder of x divided by 2^n
20+
*/
21+
public static int moduloPowerOfTwo(int x, int n) {
22+
if (n <= 0) {
23+
throw new IllegalArgumentException("The exponent must be positive");
24+
}
25+
26+
return x & ((1 << n) - 1);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to find the next higher number
5+
* with the same number of set bits as the given number.
6+
*
7+
* @author Hardvan
8+
*/
9+
public final class NextHigherSameBitCount {
10+
private NextHigherSameBitCount() {
11+
}
12+
13+
/**
14+
* Finds the next higher integer with the same number of set bits.
15+
* Steps:
16+
* 1. Find {@code c}, the rightmost set bit of {@code n}.
17+
* 2. Find {@code r}, the rightmost set bit of {@code n + c}.
18+
* 3. Swap the bits of {@code r} and {@code n} to the right of {@code c}.
19+
* 4. Shift the bits of {@code r} and {@code n} to the right of {@code c} to the rightmost.
20+
* 5. Combine the results of steps 3 and 4.
21+
*
22+
* @param n the input number
23+
* @return the next higher integer with the same set bit count
24+
*/
25+
public static int nextHigherSameBitCount(int n) {
26+
int c = n & -n;
27+
int r = n + c;
28+
return (((r ^ n) >> 2) / c) | r;
29+
}
30+
}

src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find Non Repeating Number
4+
* A utility class to find the non-repeating number in an array where every other number repeats.
5+
* This class contains a method to identify the single unique number using bit manipulation.
6+
*
7+
* The solution leverages the properties of the XOR operation, which states that:
8+
* - x ^ x = 0 for any integer x (a number XORed with itself is zero)
9+
* - x ^ 0 = x for any integer x (a number XORed with zero is the number itself)
10+
*
11+
* Using these properties, we can find the non-repeating number in linear time with constant space.
12+
*
13+
* Example:
14+
* Given the input array [2, 3, 5, 2, 3], the output will be 5 since it does not repeat.
15+
*
516
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
617
*/
7-
818
public final class NonRepeatingNumberFinder {
919
private NonRepeatingNumberFinder() {
1020
}
1121

22+
/**
23+
* Finds the non-repeating number in the given array.
24+
*
25+
* @param arr an array of integers where every number except one appears twice
26+
* @return the integer that appears only once in the array or 0 if the array is empty
27+
*/
1228
public static int findNonRepeatingNumber(int[] arr) {
1329
int result = 0;
1430
for (int num : arr) {
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find the Number Appearing Odd Times in an array
4+
* This class provides a method to find the element that appears an
5+
* odd number of times in an array. All other elements in the array
6+
* must appear an even number of times for the logic to work.
7+
*
8+
* The solution uses the XOR operation, which has the following properties:
9+
* - a ^ a = 0 (XOR-ing the same numbers cancels them out)
10+
* - a ^ 0 = a
11+
* - XOR is commutative and associative.
12+
*
13+
* Time Complexity: O(n), where n is the size of the array.
14+
* Space Complexity: O(1), as no extra space is used.
15+
*
16+
* Usage Example:
17+
* int result = NumberAppearingOddTimes.findOddOccurrence(new int[]{1, 2, 1, 2, 3});
18+
* // result will be 3
19+
*
520
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
621
*/
722

823
public final class NumberAppearingOddTimes {
924
private NumberAppearingOddTimes() {
1025
}
26+
27+
/**
28+
* Finds the element in the array that appears an odd number of times.
29+
*
30+
* @param arr the input array containing integers, where all elements
31+
* except one appear an even number of times.
32+
* @return the integer that appears an odd number of times.
33+
*/
1134
public static int findOddOccurrence(int[] arr) {
1235
int result = 0;
13-
14-
// XOR all elements in the array
1536
for (int num : arr) {
1637
result ^= num;
1738
}
18-
1939
return result;
2040
}
2141
}

src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Numbers Different Signs
4+
* This class provides a method to determine whether two integers have
5+
* different signs. It utilizes the XOR operation on the two numbers:
6+
*
7+
* - If two numbers have different signs, their most significant bits
8+
* (sign bits) will differ, resulting in a negative XOR result.
9+
* - If two numbers have the same sign, the XOR result will be non-negative.
10+
*
11+
* Time Complexity: O(1) - Constant time operation.
12+
* Space Complexity: O(1) - No extra space used.
13+
*
514
* @author Bama Charan Chhandogi
615
*/
7-
816
public final class NumbersDifferentSigns {
917
private NumbersDifferentSigns() {
1018
}
1119

20+
/**
21+
* Determines if two integers have different signs using bitwise XOR.
22+
*
23+
* @param num1 the first integer
24+
* @param num2 the second integer
25+
* @return true if the two numbers have different signs, false otherwise
26+
*/
1227
public static boolean differentSigns(int num1, int num2) {
1328
return (num1 ^ num2) < 0;
1429
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to detect if two integers
5+
* differ by exactly one bit flip.
6+
*
7+
* Example:
8+
* 1 (0001) and 2 (0010) differ by exactly one bit flip.
9+
* 7 (0111) and 3 (0011) differ by exactly one bit flip.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class OneBitDifference {
14+
private OneBitDifference() {
15+
}
16+
17+
/**
18+
* Checks if two integers differ by exactly one bit.
19+
*
20+
* @param x the first integer
21+
* @param y the second integer
22+
* @return true if x and y differ by exactly one bit, false otherwise
23+
*/
24+
public static boolean differByOneBit(int x, int y) {
25+
if (x == y) {
26+
return false;
27+
}
28+
29+
int xor = x ^ y;
30+
return (xor & (xor - 1)) == 0;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
11
package com.thealgorithms.bitmanipulation;
22

3-
/*
3+
/**
4+
* A utility class for performing single-bit operations on integers.
5+
* These operations include flipping, setting, clearing, and getting
6+
* individual bits at specified positions.
7+
*
8+
* Bit positions are zero-indexed (i.e., the least significant bit is at position 0).
9+
* These methods leverage bitwise operations for optimal performance.
10+
*
11+
* Examples:
12+
* - `flipBit(3, 1)` flips the bit at index 1 in binary `11` (result: `1`).
13+
* - `setBit(4, 0)` sets the bit at index 0 in `100` (result: `101` or 5).
14+
* - `clearBit(7, 1)` clears the bit at index 1 in `111` (result: `101` or 5).
15+
* - `getBit(6, 0)` checks if the least significant bit is set (result: `0`).
16+
*
17+
* Time Complexity: O(1) for all operations.
18+
*
419
* Author: lukasb1b (https://github.com/lukasb1b)
520
*/
6-
721
public final class SingleBitOperations {
822
private SingleBitOperations() {
923
}
24+
1025
/**
11-
* Flip the bit at position 'bit' in 'num'
26+
* Flips (toggles) the bit at the specified position.
27+
*
28+
* @param num the input number
29+
* @param bit the position of the bit to flip (0-indexed)
30+
* @return the new number after flipping the specified bit
1231
*/
1332
public static int flipBit(final int num, final int bit) {
1433
return num ^ (1 << bit);
1534
}
35+
1636
/**
17-
* Set the bit at position 'bit' to 1 in the 'num' variable
37+
* Sets the bit at the specified position to 1.
38+
*
39+
* @param num the input number
40+
* @param bit the position of the bit to set (0-indexed)
41+
* @return the new number after setting the specified bit to 1
1842
*/
1943
public static int setBit(final int num, final int bit) {
2044
return num | (1 << bit);
2145
}
46+
2247
/**
23-
* Clears the bit located at 'bit' from 'num'
48+
* Clears the bit at the specified position (sets it to 0).
49+
*
50+
* @param num the input number
51+
* @param bit the position of the bit to clear (0-indexed)
52+
* @return the new number after clearing the specified bit
2453
*/
2554
public static int clearBit(final int num, final int bit) {
2655
return num & ~(1 << bit);
2756
}
57+
2858
/**
29-
* Get the bit located at 'bit' from 'num'
59+
* Gets the bit value (0 or 1) at the specified position.
60+
*
61+
* @param num the input number
62+
* @param bit the position of the bit to retrieve (0-indexed)
63+
* @return 1 if the bit is set, 0 otherwise
3064
*/
3165
public static int getBit(final int num, final int bit) {
32-
return ((num >> bit) & 1);
66+
return (num >> bit) & 1;
3367
}
3468
}

0 commit comments

Comments
 (0)