mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-12 11:15:12 +00:00
Added extensions to byte for getting and setting bits
This commit is contained in:
42
ICD.Common.Utils/Extensions/ByteExtensions.cs
Normal file
42
ICD.Common.Utils/Extensions/ByteExtensions.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user