Skip to content

Commit 4a670d9

Browse files
rabietsiriak
authored andcommitted
Create Int2Binary (#113)
* Create Int2Binary * Added Int2Binary Tests
1 parent 9e3337e commit 4a670d9

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using Algorithms.Other;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Other
6+
{
7+
public static class Int2BinaryTests
8+
{
9+
[Test]
10+
[TestCase((UInt16) 0, "0000000000000000")]
11+
[TestCase((UInt16) 0b1, "0000000000000001")]
12+
[TestCase((UInt16) 0b0001010100111000, "0001010100111000")]
13+
[TestCase((UInt16) 0b1110111100110010, "1110111100110010")]
14+
[TestCase((UInt16)(UInt16.MaxValue - 1), "1111111111111110")]
15+
[TestCase(UInt16.MaxValue , "1111111111111111")]
16+
public static void GetsBinary(UInt16 input, string expected)
17+
{
18+
// Arrange
19+
20+
// Act
21+
var result = Int2Binary.Int2bin(input);
22+
23+
// Assert
24+
Assert.AreEqual(expected, result);
25+
}
26+
27+
28+
[Test]
29+
[TestCase((UInt32)0, "00000000000000000000000000000000")]
30+
[TestCase((UInt32)0b1, "00000000000000000000000000000001")]
31+
[TestCase((UInt32)0b0001010100111000, "00000000000000000001010100111000")]
32+
[TestCase((UInt32)0b1110111100110010, "00000000000000001110111100110010")]
33+
[TestCase((UInt32)0b10101100001110101110111100110010, "10101100001110101110111100110010")]
34+
[TestCase((UInt32)(UInt32.MaxValue - 1), "11111111111111111111111111111110")]
35+
[TestCase(UInt32.MaxValue, "11111111111111111111111111111111")]
36+
public static void GetsBinary(UInt32 input, string expected)
37+
{
38+
// Arrange
39+
40+
// Act
41+
var result = Int2Binary.Int2bin(input);
42+
43+
// Assert
44+
Assert.AreEqual(expected, result);
45+
}
46+
47+
[Test]
48+
[TestCase((UInt64)0, "0000000000000000000000000000000000000000000000000000000000000000")]
49+
[TestCase((UInt64)0b1, "0000000000000000000000000000000000000000000000000000000000000001")]
50+
[TestCase((UInt64)0b0001010100111000, "0000000000000000000000000000000000000000000000000001010100111000")]
51+
[TestCase((UInt64)0b1110111100110010, "0000000000000000000000000000000000000000000000001110111100110010")]
52+
[TestCase((UInt64)0b10101100001110101110111100110010, "0000000000000000000000000000000010101100001110101110111100110010")]
53+
[TestCase((UInt64)0b1000101110100101000011010101110101010101110101001010000011111000, "1000101110100101000011010101110101010101110101001010000011111000")]
54+
[TestCase((UInt64)(UInt64.MaxValue - 1), "1111111111111111111111111111111111111111111111111111111111111110")]
55+
[TestCase(UInt64.MaxValue, "1111111111111111111111111111111111111111111111111111111111111111")]
56+
public static void GetsBinary(UInt64 input, string expected)
57+
{
58+
// Arrange
59+
60+
// Act
61+
var result = Int2Binary.Int2bin(input);
62+
63+
// Assert
64+
Assert.AreEqual(expected, result);
65+
}
66+
67+
}
68+
}

Algorithms/Other/Int2Binary.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
3+
namespace Algorithms.Other
4+
{
5+
/// <summary>
6+
/// Manually converts an integer of certain size to a string of the binary representation.
7+
/// </summary>
8+
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1121:Use built-in type alias", Justification = "Built-in type aliases aren't as clear as concerning the amount of bits")]
9+
public static class Int2Binary
10+
{
11+
/// <summary>
12+
/// Returns string of the binary representation of given Int.
13+
/// </summary>
14+
/// <param name="input">Number to be converted.</param>
15+
/// <returns>Binary representation of input.</returns>
16+
public static string Int2bin(UInt16 input)
17+
{
18+
UInt16 msb = UInt16.MaxValue / 2 + 1;
19+
String output = string.Empty;
20+
for (int i = 0; i < 16; i++)
21+
{
22+
if (input >= msb)
23+
{
24+
output += "1";
25+
input -= msb;
26+
msb /= 2;
27+
}
28+
else
29+
{
30+
output += "0";
31+
msb /= 2;
32+
}
33+
}
34+
35+
return output;
36+
}
37+
38+
/// <summary>
39+
/// Returns string of the binary representation of given Int.
40+
/// </summary>
41+
/// <param name="input">Number to be converted.</param>
42+
/// <returns>Binary representation of input.</returns>
43+
public static string Int2bin(UInt32 input)
44+
{
45+
UInt32 msb = UInt32.MaxValue / 2 + 1;
46+
String output = string.Empty;
47+
for (int i = 0; i < 32; i++)
48+
{
49+
if (input >= msb)
50+
{
51+
output += "1";
52+
input -= msb;
53+
msb /= 2;
54+
}
55+
else
56+
{
57+
output += "0";
58+
msb /= 2;
59+
}
60+
}
61+
62+
return output;
63+
}
64+
65+
/// <summary>
66+
/// Returns string of the binary representation of given Int.
67+
/// </summary>
68+
/// <param name="input">Number to be converted.</param>
69+
/// <returns>Binary representation of input.</returns>
70+
public static string Int2bin(UInt64 input)
71+
{
72+
UInt64 msb = UInt64.MaxValue / 2 + 1;
73+
String output = string.Empty;
74+
for (int i = 0; i < 64; i++)
75+
{
76+
if (input >= msb)
77+
{
78+
output += "1";
79+
input -= msb;
80+
msb /= 2;
81+
}
82+
else
83+
{
84+
output += "0";
85+
msb /= 2;
86+
}
87+
}
88+
89+
return output;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)