-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/bin/ | ||
.classpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>src</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package biDirectionalCodeConverter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
public class BinaryToGrey { | ||
|
||
public static int toGrey(int decimal) { | ||
|
||
int i = 0; | ||
int greyInteger = 0; | ||
int binary = DecimalToBinary.toBinary(decimal); | ||
|
||
ArrayList<Integer> binaryArrayList = new ArrayList<>(); | ||
ArrayList<Integer> greyArrayList = new ArrayList<>(); | ||
|
||
while (binary > 0) { | ||
binaryArrayList.add(i++, binary % 10); | ||
binary /= 10; | ||
} | ||
|
||
Collections.reverse(binaryArrayList); | ||
|
||
for (int j = 0; j < binaryArrayList.size(); j++) { | ||
if (j == 0) { | ||
greyArrayList.add(j, binaryArrayList.get(j)); | ||
} else { | ||
greyArrayList.add(j, (binaryArrayList.get(j - 1) ^ binaryArrayList.get(j))); | ||
} | ||
} | ||
|
||
for (int j = 0; j < greyArrayList.size(); j++) { | ||
greyInteger = greyArrayList.get(j) + greyInteger * 10; | ||
} | ||
return greyInteger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package biDirectionalCodeConverter; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class DecimalToBinary { | ||
|
||
public static int toBinary(int decimal) { | ||
|
||
int i = 0; | ||
int binaryInteger = 0; | ||
ArrayList<Integer> binary = new ArrayList<>(); | ||
while (decimal > 0) { | ||
binary.add(i++, decimal % 2); | ||
decimal /= 2; | ||
} | ||
|
||
for (int j = binary.size() - 1; j >= 0; j--) { | ||
binaryInteger = binary.get(j) + binaryInteger * 10; | ||
} | ||
return binaryInteger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package biDirectionalCodeConverter; | ||
|
||
public class Main { | ||
public static void main(String args[]) { | ||
|
||
for (int i = 0; i < 16; i++) { | ||
int binary = DecimalToBinary.toBinary(i); | ||
int grey = BinaryToGrey.toGrey(i); | ||
|
||
System.out.print("The binary code of the decimal number " + i + " is "); | ||
System.out.println(String.format("%04d", binary).substring(0)); | ||
System.out.print("The grey code of the decimal number " + i + " is "); | ||
System.out.println(String.format("%04d", grey).substring(0)); | ||
} | ||
} | ||
} |