Skip to content

Commit 6af7f7b

Browse files
Add Xs3Conversion algorithm (#5743)
1 parent 6682c7c commit 6af7f7b

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
4848
* [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java)
4949
* [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java)
50+
* [Xs3Conversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/Xs3Conversion.java)
5051
* ciphers
5152
* a5
5253
* [A5Cipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/A5Cipher.java)
@@ -679,6 +680,7 @@
679680
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
680681
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
681682
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
683+
* [Xs3ConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/Xs3ConversionTest.java)
682684
* ciphers
683685
* a5
684686
* [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java)
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the Xs3Conversion class.
9+
*/
10+
public class Xs3ConversionTest {
11+
12+
/**
13+
* Test the xs3ToBinary method with an XS-3 number.
14+
*/
15+
@Test
16+
public void testXs3ToBinary() {
17+
int binary = Xs3Conversion.xs3ToBinary(0x4567);
18+
assertEquals(1234, binary); // XS-3 0x4567 should convert to binary 1234
19+
}
20+
21+
/**
22+
* Test the binaryToXs3 method with a binary number.
23+
*/
24+
@Test
25+
public void testBinaryToXs3() {
26+
int xs3 = Xs3Conversion.binaryToXs3(1234);
27+
assertEquals(0x4567, xs3); // Binary 1234 should convert to XS-3 0x4567
28+
}
29+
30+
/**
31+
* Test the xs3ToBinary method with zero.
32+
*/
33+
@Test
34+
public void testXs3ToBinaryZero() {
35+
int binary = Xs3Conversion.xs3ToBinary(0x0);
36+
assertEquals(0, binary); // XS-3 0x0 should convert to binary 0
37+
}
38+
39+
/**
40+
* Test the binaryToXs3 method with zero.
41+
*/
42+
@Test
43+
public void testBinaryToXs3Zero() {
44+
int xs3 = Xs3Conversion.binaryToXs3(0);
45+
assertEquals(0x0, xs3); // Binary 0 should convert to XS-3 0x0
46+
}
47+
48+
/**
49+
* Test the xs3ToBinary method with a single digit XS-3 number.
50+
*/
51+
@Test
52+
public void testXs3ToBinarySingleDigit() {
53+
int binary = Xs3Conversion.xs3ToBinary(0x5);
54+
assertEquals(2, binary); // XS-3 0x5 should convert to binary 2
55+
}
56+
57+
/**
58+
* Test the binaryToXs3 method with a single digit binary number.
59+
*/
60+
@Test
61+
public void testBinaryToXs3SingleDigit() {
62+
int xs3 = Xs3Conversion.binaryToXs3(2);
63+
assertEquals(0x5, xs3); // Binary 2 should convert to XS-3 0x5
64+
}
65+
}

0 commit comments

Comments
 (0)