Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c015fc

Browse files
authored
Merge pull requestTheAlgorithms#734 from nbdgit/Development
Added Binary to Gray code conversion
2 parents8e8e14d +f5754d3 commit0c015fc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packagesrc.main.java.com.conversions;
2+
3+
publicclassBinaryToGray
4+
{
5+
/*This method convert the binary number into gray code
6+
@param binarycode need to convert binary number into gray code
7+
@return graycode return as string
8+
*/
9+
10+
publicStringbinaryToGray(Stringbinarycode)
11+
{
12+
13+
StringBuildergraycode =newStringBuilder(Character.toString(binarycode.charAt(0)));
14+
15+
for(inti =0;i <binarycode.length() -1;i++)
16+
{
17+
18+
if (binarycode.charAt(i) ==binarycode.charAt(i+1))
19+
graycode.append("0");
20+
else
21+
graycode.append("1");
22+
23+
}
24+
25+
returngraycode.toString();
26+
}
27+
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagesrc.test.java.com.conversions;
2+
3+
importsrc.main.java.com.conversions.BinaryToGray;
4+
importorg.junit.Test;
5+
importstaticorg.junit.Assert.assertEquals;
6+
7+
publicclassBinaryToGrayTest
8+
{
9+
10+
@Test
11+
publicvoidtestBinaryToGray()
12+
{
13+
BinaryToGraybtog =newBinaryToGray();
14+
assertEquals("1101",btog.binaryToGray("1001"));
15+
assertEquals("11010011101",btog.binaryToGray("10011101001"));
16+
}
17+
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp