Added extensions to byte for getting and setting bits

This commit is contained in:
jeffery.thompson
2017-07-18 09:58:55 -04:00
parent ad0c0a637b
commit 4801d45347
2 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
namespace ICD.Common.Utils.Extensions
{
public static class ByteExtensions
{
public static bool GetBit(this byte b, int n)
{
if (n > 7 || n < 0)
throw new ArgumentException();
return (b & (1 << n)) == (1 << n);
}
public static byte SetBit(this byte b, int n, bool v)
{
if (v)
return SetBitOn(b, n);
return SetBitOff(b, n);
}
public static byte SetBitOn(this byte b, int n)
{
if (n > 7 || n < 0)
throw new ArgumentException();
return (byte)(b | (1 << n));
}
public static byte SetBitOff(this byte b, int n)
{
if (n > 7 || n < 0)
throw new ArgumentException();
return (byte)(b & ~(1 << n));
}
}
}

View File

@@ -84,6 +84,7 @@
<Compile Include="EventArguments\UShortEventArgs.cs" />
<Compile Include="EventArguments\XmlRecursionEventArgs.cs" />
<None Include="ObfuscationSettings.cs" />
<Compile Include="Extensions\ByteExtensions.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\Logging\ILoggerService.cs" />