From 4801d453472759ef567fde4c48d9860c1965d111 Mon Sep 17 00:00:00 2001 From: "jeffery.thompson" Date: Tue, 18 Jul 2017 09:58:55 -0400 Subject: [PATCH] Added extensions to byte for getting and setting bits --- ICD.Common.Utils/Extensions/ByteExtensions.cs | 42 +++++++++++++++++++ .../ICD.Common.Utils_SimplSharp.csproj | 1 + 2 files changed, 43 insertions(+) create mode 100644 ICD.Common.Utils/Extensions/ByteExtensions.cs diff --git a/ICD.Common.Utils/Extensions/ByteExtensions.cs b/ICD.Common.Utils/Extensions/ByteExtensions.cs new file mode 100644 index 0000000..42d8787 --- /dev/null +++ b/ICD.Common.Utils/Extensions/ByteExtensions.cs @@ -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)); + } + } +} \ No newline at end of file diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index d2ae64b..c39b3ae 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -84,6 +84,7 @@ +