Skip to content

Commit be08838

Browse files
authored
Merge branch 'master' into master
2 parents a157a27 + bcf4034 commit be08838

File tree

168 files changed

+8975
-810
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

+8975
-810
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
uses: actions/setup-java@v4
1111
with:
1212
java-version: 21
13-
distribution: 'adopt'
13+
distribution: 'temurin'
1414
- name: Build with Maven
1515
run: mvn --batch-mode --update-snapshots verify
1616
- name: Upload coverage to codecov (tokenless)

.github/workflows/codeql.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: actions/setup-java@v4
3131
with:
3232
java-version: 21
33-
distribution: 'adopt'
33+
distribution: 'temurin'
3434

3535
- name: Initialize CodeQL
3636
uses: github/codeql-action/init@v3

.github/workflows/infer.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/setup-java@v4
1919
with:
2020
java-version: 21
21-
distribution: 'adopt'
21+
distribution: 'temurin'
2222

2323
- name: Set up OCaml
2424
uses: ocaml/setup-ocaml@v3

DIRECTORY.md

+121
Large diffs are not rendered by default.

checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
<!-- See https://checkstyle.org/checks/misc/index.html -->
184184
<module name="ArrayTypeStyle"/>
185185
<!-- TODO <module name="FinalParameters"/> -->
186-
<!-- TODO <module name="TodoComment"/> -->
186+
<module name="TodoComment"/>
187187
<module name="UpperEll"/>
188188

189189
<!-- https://checkstyle.org/filters/suppressionxpathfilter.html -->

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
<version>${assertj.version}</version>
4141
<scope>test</scope>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.mockito</groupId>
45+
<artifactId>mockito-core</artifactId>
46+
<version>5.14.1</version>
47+
<scope>test</scope>
48+
</dependency>
49+
4350

4451
<dependency>
4552
<groupId>org.junit.jupiter</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers.
5+
*
6+
* BCD is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight.
7+
*
8+
* For more information, refer to the
9+
* <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
10+
*
11+
* <b>Example usage:</b>
12+
* <pre>
13+
* int decimal = BcdConversion.bcdToDecimal(0x1234);
14+
* System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
15+
*
16+
* int bcd = BcdConversion.decimalToBcd(1234);
17+
* System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
18+
* </pre>
19+
*/
20+
public final class BcdConversion {
21+
private BcdConversion() {
22+
}
23+
24+
/**
25+
* Converts a BCD (Binary-Coded Decimal) number to a decimal number.
26+
* <p>Steps:
27+
* <p>1. Validate the BCD number to ensure all digits are between 0 and 9.
28+
* <p>2. Extract the last 4 bits (one BCD digit) from the BCD number.
29+
* <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number.
30+
* <p>4. Shift the BCD number right by 4 bits to process the next BCD digit.
31+
* <p>5. Repeat steps 1-4 until the BCD number is zero.
32+
*
33+
* @param bcd The BCD number.
34+
* @return The corresponding decimal number.
35+
* @throws IllegalArgumentException if the BCD number contains invalid digits.
36+
*/
37+
public static int bcdToDecimal(int bcd) {
38+
int decimal = 0;
39+
int multiplier = 1;
40+
41+
// Validate BCD digits
42+
while (bcd > 0) {
43+
int digit = bcd & 0xF;
44+
if (digit > 9) {
45+
throw new IllegalArgumentException("Invalid BCD digit: " + digit);
46+
}
47+
decimal += digit * multiplier;
48+
multiplier *= 10;
49+
bcd >>= 4;
50+
}
51+
return decimal;
52+
}
53+
54+
/**
55+
* Converts a decimal number to BCD (Binary-Coded Decimal).
56+
* <p>Steps:
57+
* <p>1. Check if the decimal number is within the valid range for BCD (0 to 9999).
58+
* <p>2. Extract the last decimal digit from the decimal number.
59+
* <p>3. Shift the digit to the correct BCD position and add it to the BCD number.
60+
* <p>4. Remove the last decimal digit from the decimal number.
61+
* <p>5. Repeat steps 2-4 until the decimal number is zero.
62+
*
63+
* @param decimal The decimal number.
64+
* @return The corresponding BCD number.
65+
* @throws IllegalArgumentException if the decimal number is greater than 9999.
66+
*/
67+
public static int decimalToBcd(int decimal) {
68+
if (decimal < 0 || decimal > 9999) {
69+
throw new IllegalArgumentException("Value out of bounds for BCD representation: " + decimal);
70+
}
71+
72+
int bcd = 0;
73+
int shift = 0;
74+
while (decimal > 0) {
75+
int digit = decimal % 10;
76+
bcd |= (digit << (shift * 4));
77+
decimal /= 10;
78+
shift++;
79+
}
80+
return bcd;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class contains a method to check if the binary representation of a number is a palindrome.
5+
* <p>
6+
* A binary palindrome is a number whose binary representation is the same when read from left to right and right to left.
7+
* For example, the number 9 has a binary representation of 1001, which is a palindrome.
8+
* The number 10 has a binary representation of 1010, which is not a palindrome.
9+
* </p>
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class BinaryPalindromeCheck {
14+
private BinaryPalindromeCheck() {
15+
}
16+
17+
/**
18+
* Checks if the binary representation of a number is a palindrome.
19+
*
20+
* @param x The number to check.
21+
* @return True if the binary representation is a palindrome, otherwise false.
22+
*/
23+
public static boolean isBinaryPalindrome(int x) {
24+
int reversed = reverseBits(x);
25+
return x == reversed;
26+
}
27+
28+
/**
29+
* Helper function to reverse all the bits of an integer.
30+
*
31+
* @param x The number to reverse the bits of.
32+
* @return The number with reversed bits.
33+
*/
34+
private static int reverseBits(int x) {
35+
int result = 0;
36+
while (x > 0) {
37+
result <<= 1;
38+
result |= (x & 1);
39+
x >>= 1;
40+
}
41+
return result;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR)
7+
*/
8+
public final class BooleanAlgebraGates {
9+
10+
private BooleanAlgebraGates() {
11+
// Prevent instantiation
12+
}
13+
14+
/**
15+
* Represents a Boolean gate that takes multiple inputs and returns a result.
16+
*/
17+
interface BooleanGate {
18+
/**
19+
* Evaluates the gate with the given inputs.
20+
*
21+
* @param inputs The input values for the gate.
22+
* @return The result of the evaluation.
23+
*/
24+
boolean evaluate(List<Boolean> inputs);
25+
}
26+
27+
/**
28+
* AND Gate implementation.
29+
* Returns true if all inputs are true; otherwise, false.
30+
*/
31+
static class ANDGate implements BooleanGate {
32+
@Override
33+
public boolean evaluate(List<Boolean> inputs) {
34+
for (boolean input : inputs) {
35+
if (!input) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}
42+
43+
/**
44+
* OR Gate implementation.
45+
* Returns true if at least one input is true; otherwise, false.
46+
*/
47+
static class ORGate implements BooleanGate {
48+
@Override
49+
public boolean evaluate(List<Boolean> inputs) {
50+
for (boolean input : inputs) {
51+
if (input) {
52+
return true;
53+
}
54+
}
55+
return false;
56+
}
57+
}
58+
59+
/**
60+
* NOT Gate implementation (Unary operation).
61+
* Negates a single input value.
62+
*/
63+
static class NOTGate {
64+
/**
65+
* Evaluates the negation of the input.
66+
*
67+
* @param input The input value to be negated.
68+
* @return The negated value.
69+
*/
70+
public boolean evaluate(boolean input) {
71+
return !input;
72+
}
73+
}
74+
75+
/**
76+
* XOR Gate implementation.
77+
* Returns true if an odd number of inputs are true; otherwise, false.
78+
*/
79+
static class XORGate implements BooleanGate {
80+
@Override
81+
public boolean evaluate(List<Boolean> inputs) {
82+
boolean result = false;
83+
for (boolean input : inputs) {
84+
result ^= input;
85+
}
86+
return result;
87+
}
88+
}
89+
90+
/**
91+
* NAND Gate implementation.
92+
* Returns true if at least one input is false; otherwise, false.
93+
*/
94+
static class NANDGate implements BooleanGate {
95+
@Override
96+
public boolean evaluate(List<Boolean> inputs) {
97+
return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND
98+
}
99+
}
100+
101+
/**
102+
* NOR Gate implementation.
103+
* Returns true if all inputs are false; otherwise, false.
104+
*/
105+
static class NORGate implements BooleanGate {
106+
@Override
107+
public boolean evaluate(List<Boolean> inputs) {
108+
return !new ORGate().evaluate(inputs); // Equivalent to negation of OR
109+
}
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* ClearLeftmostSetBit class contains a method to clear the leftmost set bit of a number.
5+
* The leftmost set bit is the leftmost bit that is set to 1 in the binary representation of a number.
6+
*
7+
* Example:
8+
* 26 (11010) -> 10 (01010)
9+
* 1 (1) -> 0 (0)
10+
* 7 (111) -> 3 (011)
11+
* 6 (0110) -> 2 (0010)
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class ClearLeftmostSetBit {
16+
private ClearLeftmostSetBit() {
17+
}
18+
19+
/**
20+
* Clears the leftmost set bit (1) of a given number.
21+
* Step 1: Find the position of the leftmost set bit
22+
* Step 2: Create a mask with all bits set except for the leftmost set bit
23+
* Step 3: Clear the leftmost set bit using AND with the mask
24+
*
25+
* @param num The input number.
26+
* @return The number after clearing the leftmost set bit.
27+
*/
28+
public static int clearLeftmostSetBit(int num) {
29+
int pos = 0;
30+
int temp = num;
31+
while (temp > 0) {
32+
temp >>= 1;
33+
pos++;
34+
}
35+
36+
int mask = ~(1 << (pos - 1));
37+
return num & mask;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* CountLeadingZeros class contains a method to count the number of leading zeros in the binary representation of a number.
5+
* The number of leading zeros is the number of zeros before the leftmost 1 bit.
6+
* For example, the number 5 has 29 leading zeros in its 32-bit binary representation.
7+
* The number 0 has 32 leading zeros.
8+
* The number 1 has 31 leading zeros.
9+
* The number -1 has no leading zeros.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class CountLeadingZeros {
14+
private CountLeadingZeros() {
15+
}
16+
17+
/**
18+
* Counts the number of leading zeros in the binary representation of a number.
19+
* Method: Keep shifting the mask to the right until the leftmost bit is 1.
20+
* The number of shifts is the number of leading zeros.
21+
*
22+
* @param num The input number.
23+
* @return The number of leading zeros.
24+
*/
25+
public static int countLeadingZeros(int num) {
26+
if (num == 0) {
27+
return 32;
28+
}
29+
30+
int count = 0;
31+
int mask = 1 << 31;
32+
while ((mask & num) == 0) {
33+
count++;
34+
mask >>>= 1;
35+
}
36+
37+
return count;
38+
}
39+
}

0 commit comments

Comments
 (0)