Skip to content

Commit 76be93d

Browse files
authored
Merge branch 'master' into rsa_improve
2 parents b891ca0 + 70adf6f commit 76be93d

File tree

162 files changed

+7989
-1465
lines changed

Some content is hidden

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

162 files changed

+7989
-1465
lines changed

DIRECTORY.md

Lines changed: 76 additions & 1 deletion
Large diffs are not rendered by default.

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.11.2</version>
23+
<version>5.11.3</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.11.2</version>
34+
<version>5.11.3</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -51,7 +51,7 @@
5151
<dependency>
5252
<groupId>org.junit.jupiter</groupId>
5353
<artifactId>junit-jupiter-api</artifactId>
54-
<version>5.11.2</version>
54+
<version>5.11.3</version>
5555
<scope>test</scope>
5656
</dependency>
5757
<dependency>
@@ -132,7 +132,7 @@
132132
<plugin>
133133
<groupId>com.github.spotbugs</groupId>
134134
<artifactId>spotbugs-maven-plugin</artifactId>
135-
<version>4.8.6.4</version>
135+
<version>4.8.6.5</version>
136136
<configuration>
137137
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
138138
<includeTests>true</includeTests>
Lines changed: 33 additions & 0 deletions
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+
}

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

Lines changed: 16 additions & 2 deletions
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;
Lines changed: 28 additions & 0 deletions
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+
}

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

Lines changed: 18 additions & 2 deletions
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) {
Lines changed: 24 additions & 4 deletions
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

Lines changed: 17 additions & 2 deletions
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
}
Lines changed: 32 additions & 0 deletions
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+
}
Lines changed: 41 additions & 7 deletions
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)