Skip to content

Commit 343035b

Browse files
authored
Merge branch 'master' into lottery_new_algo
2 parents 62f36cb + 213fd5a commit 343035b

29 files changed

+1688
-35
lines changed

DIRECTORY.md

Lines changed: 21 additions & 0 deletions
Large diffs are not rendered by default.

checkstyle.xml

Lines changed: 1 addition & 1 deletion
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 -->
Lines changed: 82 additions & 0 deletions
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+
}
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: 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: 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 XS-3 (Excess-3) and binary.
5+
*
6+
* Excess-3, also called XS-3, is a binary-coded decimal (BCD) code in which each decimal digit is represented by its corresponding 4-bit binary value plus 3.
7+
*
8+
* For more information, refer to the
9+
* <a href="https://en.wikipedia.org/wiki/Excess-3">Excess-3</a> Wikipedia page.
10+
*
11+
* <b>Example usage:</b>
12+
* <pre>
13+
* int binary = Xs3Conversion.xs3ToBinary(0x4567);
14+
* System.out.println("XS-3 0x4567 to binary: " + binary); // Output: 1234
15+
*
16+
* int xs3 = Xs3Conversion.binaryToXs3(1234);
17+
* System.out.println("Binary 1234 to XS-3: " + Integer.toHexString(xs3)); // Output: 0x4567
18+
* </pre>
19+
*/
20+
public final class Xs3Conversion {
21+
private Xs3Conversion() {
22+
}
23+
/**
24+
* Converts an XS-3 (Excess-3) number to binary.
25+
*
26+
* @param xs3 The XS-3 number.
27+
* @return The corresponding binary number.
28+
*/
29+
public static int xs3ToBinary(int xs3) {
30+
int binary = 0;
31+
int multiplier = 1;
32+
while (xs3 > 0) {
33+
int digit = (xs3 & 0xF) - 3; // Extract the last 4 bits (one XS-3 digit) and subtract 3
34+
binary += digit * multiplier;
35+
multiplier *= 10;
36+
xs3 >>= 4; // Shift right by 4 bits to process the next XS-3 digit
37+
}
38+
return binary;
39+
}
40+
41+
/**
42+
* Converts a binary number to XS-3 (Excess-3).
43+
*
44+
* @param binary The binary number.
45+
* @return The corresponding XS-3 number.
46+
*/
47+
public static int binaryToXs3(int binary) {
48+
int xs3 = 0;
49+
int shift = 0;
50+
while (binary > 0) {
51+
int digit = (binary % 10) + 3; // Extract the last decimal digit and add 3
52+
xs3 |= (digit << (shift * 4)); // Shift the digit to the correct XS-3 position
53+
binary /= 10; // Remove the last decimal digit
54+
shift++;
55+
}
56+
return xs3;
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* Converts an IPv4 address to its binary equivalent and vice-versa.
5+
* IP to Binary: Converts an IPv4 address to its binary equivalent.
6+
* Example: 127.3.4.5 -> 01111111.00000011.00000100.00000101
7+
*
8+
* Binary to IP: Converts a binary equivalent to an IPv4 address.
9+
* Example: 01111111.00000011.00000100.00000101 -> 127.3.4.5
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class IPConverter {
14+
private IPConverter() {
15+
}
16+
17+
/**
18+
* Converts an IPv4 address to its binary equivalent.
19+
* @param ip The IPv4 address to convert.
20+
* @return The binary equivalent of the IPv4 address.
21+
*/
22+
public static String ipToBinary(String ip) {
23+
StringBuilder binary = new StringBuilder();
24+
for (String octet : ip.split("\\.")) {
25+
binary.append(octetToBinary(Integer.parseInt(octet))).append(".");
26+
}
27+
return binary.substring(0, binary.length() - 1);
28+
}
29+
30+
/**
31+
* Converts a single octet to its 8-bit binary representation.
32+
* @param octet The octet to convert (0-255).
33+
* @return The 8-bit binary representation as a String.
34+
*/
35+
private static String octetToBinary(int octet) {
36+
char[] binary = {'0', '0', '0', '0', '0', '0', '0', '0'};
37+
for (int i = 7; i >= 0; i--) {
38+
if ((octet & 1) == 1) {
39+
binary[i] = '1';
40+
}
41+
octet >>>= 1;
42+
}
43+
return new String(binary);
44+
}
45+
46+
/**
47+
* Converts a binary equivalent to an IPv4 address.
48+
* @param binary The binary equivalent to convert.
49+
* @return The IPv4 address of the binary equivalent.
50+
*/
51+
public static String binaryToIP(String binary) {
52+
StringBuilder ip = new StringBuilder();
53+
for (String octet : binary.split("\\.")) {
54+
ip.append(Integer.parseInt(octet, 2)).append(".");
55+
}
56+
return ip.substring(0, ip.length() - 1);
57+
}
58+
}

src/main/java/com/thealgorithms/datastructures/graphs/Cycles.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
class Cycle {
77

88
private final int nodes;
9-
private final int edges;
109
private int[][] adjacencyMatrix;
1110
private boolean[] visited;
1211
ArrayList<ArrayList<Integer>> cycles = new ArrayList<ArrayList<Integer>>();
@@ -16,7 +15,7 @@ class Cycle {
1615
System.out.print("Enter the no. of nodes: ");
1716
nodes = in.nextInt();
1817
System.out.print("Enter the no. of Edges: ");
19-
edges = in.nextInt();
18+
final int edges = in.nextInt();
2019

2120
adjacencyMatrix = new int[nodes][nodes];
2221
visited = new boolean[nodes];

0 commit comments

Comments
 (0)