Skip to content

Commit 67e3377

Browse files
authored
Merge branch 'master' into circular_buffer_improvement
2 parents eba6229 + 4a03f42 commit 67e3377

File tree

4 files changed

+297
-0
lines changed

4 files changed

+297
-0
lines changed
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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate;
6+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate;
7+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate;
8+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate;
9+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate;
10+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate;
11+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate;
12+
import java.util.Arrays;
13+
import java.util.Collections;
14+
import java.util.List;
15+
import java.util.stream.Stream;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.CsvSource;
18+
import org.junit.jupiter.params.provider.MethodSource;
19+
20+
class BooleanAlgebraGatesTest {
21+
22+
@ParameterizedTest(name = "ANDGate Test Case {index}: inputs={0} -> expected={1}")
23+
@MethodSource("provideAndGateTestCases")
24+
void testANDGate(List<Boolean> inputs, boolean expected) {
25+
BooleanGate gate = new ANDGate();
26+
assertEquals(expected, gate.evaluate(inputs));
27+
}
28+
29+
@ParameterizedTest(name = "ORGate Test Case {index}: inputs={0} -> expected={1}")
30+
@MethodSource("provideOrGateTestCases")
31+
void testORGate(List<Boolean> inputs, boolean expected) {
32+
BooleanGate gate = new ORGate();
33+
assertEquals(expected, gate.evaluate(inputs));
34+
}
35+
36+
@ParameterizedTest(name = "NOTGate Test Case {index}: input={0} -> expected={1}")
37+
@CsvSource({"true, false", "false, true"})
38+
void testNOTGate(boolean input, boolean expected) {
39+
NOTGate gate = new NOTGate();
40+
assertEquals(expected, gate.evaluate(input));
41+
}
42+
43+
@ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}")
44+
@MethodSource("provideXorGateTestCases")
45+
void testXORGate(List<Boolean> inputs, boolean expected) {
46+
BooleanGate gate = new XORGate();
47+
assertEquals(expected, gate.evaluate(inputs));
48+
}
49+
50+
@ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}")
51+
@MethodSource("provideNandGateTestCases")
52+
void testNANDGate(List<Boolean> inputs, boolean expected) {
53+
BooleanGate gate = new NANDGate();
54+
assertEquals(expected, gate.evaluate(inputs));
55+
}
56+
57+
@ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}")
58+
@MethodSource("provideNorGateTestCases")
59+
void testNORGate(List<Boolean> inputs, boolean expected) {
60+
BooleanGate gate = new NORGate();
61+
assertEquals(expected, gate.evaluate(inputs));
62+
}
63+
64+
// Helper methods to provide test data for each gate
65+
66+
static Stream<Object[]> provideAndGateTestCases() {
67+
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE},
68+
new Object[] {Collections.emptyList(), Boolean.TRUE} // AND over no inputs is true
69+
);
70+
}
71+
72+
static Stream<Object[]> provideOrGateTestCases() {
73+
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE},
74+
new Object[] {Collections.emptyList(), Boolean.FALSE} // OR over no inputs is false
75+
);
76+
}
77+
78+
static Stream<Object[]> provideXorGateTestCases() {
79+
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // XOR over odd true
80+
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // XOR over single true
81+
new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, // XOR over all false
82+
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE} // XOR over even true
83+
);
84+
}
85+
86+
static Stream<Object[]> provideNandGateTestCases() {
87+
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NAND of all true is false
88+
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE), Boolean.TRUE}, // NAND with one false is true
89+
new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NAND of all false is true
90+
new Object[] {Collections.emptyList(), Boolean.FALSE} // NAND over no inputs is false (negation of AND)
91+
);
92+
}
93+
94+
static Stream<Object[]> provideNorGateTestCases() {
95+
return Stream.of(new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NOR of all false is true
96+
new Object[] {Arrays.asList(Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // NOR with one true is false
97+
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NOR of all true is false
98+
new Object[] {Collections.emptyList(), Boolean.TRUE} // NOR over no inputs is true (negation of OR)
99+
);
100+
}
101+
}
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+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public final class FindNthBitTest {
11+
12+
/**
13+
* A parameterized test that checks the value of the Nth bit for different inputs.
14+
*
15+
* @param num the number whose Nth bit is being tested
16+
* @param n the bit position
17+
* @param expected the expected value of the Nth bit (0 or 1)
18+
*/
19+
@ParameterizedTest
20+
@MethodSource("provideTestCases")
21+
void findNthBitParameterizedTest(int num, int n, int expected) {
22+
assertEquals(expected, FindNthBit.findNthBit(num, n));
23+
}
24+
25+
/**
26+
* Provides the test cases as a stream of arguments for the parameterized test.
27+
*
28+
* @return a stream of test cases where each case consists of a number, the bit position,
29+
* and the expected result.
30+
*/
31+
private static Stream<Arguments> provideTestCases() {
32+
return Stream.of(Arguments.of(13, 2, 0), // binary: 1101, 2nd bit is 0
33+
Arguments.of(13, 3, 1), // binary: 1101, 3rd bit is 1
34+
Arguments.of(4, 2, 0), // binary: 100, 2nd bit is 0
35+
Arguments.of(4, 3, 1), // binary: 100, 3rd bit is 1
36+
Arguments.of(1, 1, 1) // binary: 1, 1st bit is 1
37+
);
38+
}
39+
}

0 commit comments

Comments
 (0)