Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BitUtil #3

Merged
merged 4 commits into from Jul 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/NTH/NTH.Tests/BitUtilTests.cs
@@ -0,0 +1,61 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NTH.Tests
{
[TestClass]
public class BitUtilTests
{
[TestMethod]
public void HiWord()
{
const int value = 0x12345678;
const short expectedValue = 0x1234;

int expected = value >> 16; // high = 0x1234
int actual = BitUtil.HighWord(value);

Assert.AreEqual(expected, expectedValue);
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void LoWord()
{
const int value = 0x12345678;
const short expectedValue = 0x5678;

int expected = value & 0xFFFF; // low = 0x5678
int actual = BitUtil.LowWord(value);

Assert.AreEqual(expected, expectedValue);
Assert.AreEqual(expected, actual);
}


[TestMethod]
public void LoByte()
{
const short value = 0x1234;
const byte expectedValue = 0x34;

int expected = value & 0xFF; // low = 0x34
int actual = BitUtil.LowByte(value);

Assert.AreEqual(expected, expectedValue);
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void HiByte()
{
const short value = 0x1234;
const byte expectedValue = 0x12;

int expected = value >> 8; // low = 0x12
int actual = BitUtil.HighByte(value);

Assert.AreEqual(expected, expectedValue);
Assert.AreEqual(expected, actual);
}
}
}
1 change: 1 addition & 0 deletions src/NTH/NTH.Tests/NTH.Tests.csproj
Expand Up @@ -69,6 +69,7 @@
</Choose>
<ItemGroup>
<Compile Include="Analysis\DiffFinderTests.cs" />
<Compile Include="BitUtilTests.cs" />
<Compile Include="ByteSizeTests.cs" />
<Compile Include="EnumExtensionsTest.cs" />
<Compile Include="MathExTests.cs" />
Expand Down
24 changes: 24 additions & 0 deletions src/NTH/NTH/BitUtil.cs
@@ -0,0 +1,24 @@

namespace NTH
{
public class BitUtil
{
public static short HighWord(int dword)
{
return (short)(dword >> 16);
}
public static short LowWord(int dword)
{
return (short)(dword & 0x0000FFFF);
}

public static byte HighByte(short word)
{
return (byte)(word >> 8);
}
public static byte LowByte(short word)
{
return (byte)(word & 0x00FF);
}
}
}
1 change: 1 addition & 0 deletions src/NTH/NTH/NTH-40-client.csproj
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Analysis\Rendering\ConsoleDiffRenderer.cs" />
<Compile Include="Analysis\Rendering\GenericConsoleDiffRenderer.cs" />
<Compile Include="Analysis\Rendering\IDiffRenderer.cs" />
<Compile Include="BitUtil.cs" />
<Compile Include="BuildMetadata.cs" />
<Compile Include="ByteSize.cs" />
<Compile Include="ByteSizeUnit.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NTH/NTH/NTH.csproj
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Analysis\Rendering\ConsoleDiffRenderer.cs" />
<Compile Include="Analysis\Rendering\GenericConsoleDiffRenderer.cs" />
<Compile Include="Analysis\Rendering\IDiffRenderer.cs" />
<Compile Include="BitUtil.cs" />
<Compile Include="BuildMetadata.cs" />
<Compile Include="ByteSizeUnit.cs" />
<Compile Include="ByteSize.cs" />
Expand Down