Skip to content

Commit 8e0cff0

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents c50b777 + 6682c7c commit 8e0cff0

40 files changed

+2237
-44
lines changed

DIRECTORY.md

Lines changed: 31 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
5+
*
6+
* Binary-Coded Decimal (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 binary = BcdConversion.bcdToBinary(0x1234);
14+
* System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
15+
*
16+
* int bcd = BcdConversion.binaryToBcd(1234);
17+
* System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
18+
* </pre>
19+
*/
20+
public final class BcdConversion {
21+
private BcdConversion() {
22+
}
23+
/**
24+
* Converts a BCD (Binary-Coded Decimal) number to binary.
25+
*
26+
* @param bcd The BCD number.
27+
* @return The corresponding binary number.
28+
*/
29+
public static int bcdToBinary(int bcd) {
30+
int binary = 0;
31+
int multiplier = 1;
32+
while (bcd > 0) {
33+
int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit)
34+
binary += digit * multiplier;
35+
multiplier *= 10;
36+
bcd >>= 4; // Shift right by 4 bits to process the next BCD digit
37+
}
38+
return binary;
39+
}
40+
41+
/**
42+
* Converts a binary number to BCD (Binary-Coded Decimal).
43+
*
44+
* @param binary The binary number.
45+
* @return The corresponding BCD number.
46+
*/
47+
public static int binaryToBcd(int binary) {
48+
int bcd = 0;
49+
int shift = 0;
50+
while (binary > 0) {
51+
int digit = binary % 10; // Extract the last decimal digit
52+
bcd |= (digit << (shift * 4)); // Shift the digit to the correct BCD position
53+
binary /= 10; // Remove the last decimal digit
54+
shift++;
55+
}
56+
return bcd;
57+
}
58+
}
Lines changed: 43 additions & 0 deletions
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+
}
Lines changed: 111 additions & 0 deletions
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+
}
Lines changed: 39 additions & 0 deletions
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+
}
Lines changed: 39 additions & 0 deletions
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+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* A utility class to find the Nth bit of a given number.
5+
*
6+
* <p>This class provides a method to extract the value of the Nth bit (either 0 or 1)
7+
* from the binary representation of a given integer.
8+
*
9+
* <p>Example:
10+
* <pre>{@code
11+
* int result = FindNthBit.findNthBit(5, 2); // returns 0 as the 2nd bit of 5 (binary 101) is 0.
12+
* }</pre>
13+
*
14+
* <p>Author: <a href="https://github.com/Tuhinm2002">Tuhinm2002</a>
15+
*/
16+
public final class FindNthBit {
17+
18+
/**
19+
* Private constructor to prevent instantiation.
20+
*
21+
* <p>This is a utility class, and it should not be instantiated.
22+
* Attempting to instantiate this class will throw an UnsupportedOperationException.
23+
*/
24+
private FindNthBit() {
25+
throw new UnsupportedOperationException("Utility class");
26+
}
27+
28+
/**
29+
* Finds the value of the Nth bit of the given number.
30+
*
31+
* <p>This method uses bitwise operations to extract the Nth bit from the
32+
* binary representation of the given integer.
33+
*
34+
* @param num the integer number whose Nth bit is to be found
35+
* @param n the bit position (1-based) to retrieve
36+
* @return the value of the Nth bit (0 or 1)
37+
* @throws IllegalArgumentException if the bit position is less than 1
38+
*/
39+
public static int findNthBit(int num, int n) {
40+
if (n < 1) {
41+
throw new IllegalArgumentException("Bit position must be greater than or equal to 1.");
42+
}
43+
// Shifting the number to the right by (n - 1) positions and checking the last bit
44+
return (num & (1 << (n - 1))) >> (n - 1);
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Gray code is a binary numeral system where two successive values differ in only one bit.
5+
* This is a simple conversion between binary and Gray code.
6+
* Example:
7+
* 7 -> 0111 -> 0100 -> 4
8+
* 4 -> 0100 -> 0111 -> 7
9+
* 0 -> 0000 -> 0000 -> 0
10+
* 1 -> 0001 -> 0000 -> 0
11+
* 2 -> 0010 -> 0011 -> 3
12+
* 3 -> 0011 -> 0010 -> 2
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class GrayCodeConversion {
17+
private GrayCodeConversion() {
18+
}
19+
20+
/**
21+
* Converts a binary number to Gray code.
22+
*
23+
* @param num The binary number.
24+
* @return The corresponding Gray code.
25+
*/
26+
public static int binaryToGray(int num) {
27+
return num ^ (num >> 1);
28+
}
29+
30+
/**
31+
* Converts a Gray code number back to binary.
32+
*
33+
* @param gray The Gray code number.
34+
* @return The corresponding binary number.
35+
*/
36+
public static int grayToBinary(int gray) {
37+
int binary = gray;
38+
while (gray > 0) {
39+
gray >>= 1;
40+
binary ^= gray;
41+
}
42+
return binary;
43+
}
44+
}

0 commit comments

Comments
 (0)