2894 lines · 113.6 KB · C#
RawDownload
//******************************************************************************************************//  BitExtensions.cs - Gbtc////  Copyright © 2010, Grid Protection Alliance.  All Rights Reserved.////  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See//  the NOTICE file distributed with this work for additional information regarding copyright ownership.//  The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may//  not use this file except in compliance with the License. You may obtain a copy of the License at:////      http://www.opensource.org/licenses/eclipse-1.0.php////  Unless agreed to in writing, the subject software distributed under the License is distributed on an//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the//  License for the specific language governing permissions and limitations.////  Code Modification History://  ----------------------------------------------------------------------------------------------------//  01/25/2008 - J. Ritchie Carroll//       Initial version of source generated.//  09/14/2009 - Stephen C. Wills//       Added new header and license agreement.//  10/8/2012 - Danyelle Gilliam//        Modified Header////******************************************************************************************************    #region [ Contributor License Agreements ] /**************************************************************************\   Copyright © 2009 - J. Ritchie Carroll, Pinal C. Patel   All rights reserved.     Redistribution and use in source and binary forms, with or without   modification, are permitted provided that the following conditions   are met:        * Redistributions of source code must retain the above copyright        notice, this list of conditions and the following disclaimer.             * Redistributions in binary form must reproduce the above        copyright notice, this list of conditions and the following        disclaimer in the documentation and/or other materials provided        with the distribution.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY   OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  \**************************************************************************/ #endregion using System; namespace GSF{    #region [ Enumerations ]     /// <summary>    /// Represents bits in a signed or unsigned integer value.    /// </summary>    [Flags(), CLSCompliant(false)]    public enum Bits : ulong    {        /// <summary>No bits set (0x0000000000000000)</summary>        Nil = 0x00000000,         // Byte 0, Bits 0-7         /// <summary>Bit 00 (0x0000000000000001)</summary>        Bit00 = 0x00000001,     // 00000001 = 1         /// <summary>Bit 01 (0x0000000000000002)</summary>        Bit01 = Bit00 << 1,     // 00000010 = 2         /// <summary>Bit 02 (0x0000000000000004)</summary>        Bit02 = Bit01 << 1,     // 00000100 = 4         /// <summary>Bit 03 (0x0000000000000008)</summary>        Bit03 = Bit02 << 1,     // 00001000 = 8         /// <summary>Bit 04 (0x0000000000000010)</summary>        Bit04 = Bit03 << 1,     // 00010000 = 16         /// <summary>Bit 05 (0x0000000000000020)</summary>        Bit05 = Bit04 << 1,     // 00100000 = 32         /// <summary>Bit 06 (0x0000000000000040)</summary>        Bit06 = Bit05 << 1,     // 01000000 = 64         /// <summary>Bit 07 (0x0000000000000080)</summary>        Bit07 = Bit06 << 1,     // 10000000 = 128         // Byte 1, Bits 8-15         /// <summary>Bit 08 (0x0000000000000100)</summary>        Bit08 = Bit07 << 1,         /// <summary>Bit 09 (0x0000000000000200)</summary>        Bit09 = Bit08 << 1,         /// <summary>Bit 10 (0x0000000000000400)</summary>        Bit10 = Bit09 << 1,         /// <summary>Bit 11 (0x0000000000000800)</summary>        Bit11 = Bit10 << 1,         /// <summary>Bit 12 (0x0000000000001000)</summary>        Bit12 = Bit11 << 1,         /// <summary>Bit 13 (0x0000000000002000)</summary>        Bit13 = Bit12 << 1,         /// <summary>Bit 14 (0x0000000000004000)</summary>        Bit14 = Bit13 << 1,         /// <summary>Bit 15 (0x0000000000008000)</summary>        Bit15 = Bit14 << 1,         // Byte 2, Bits 16-23         /// <summary>Bit 16 (0x0000000000010000)</summary>        Bit16 = Bit15 << 1,         /// <summary>Bit 17 (0x0000000000020000)</summary>        Bit17 = Bit16 << 1,         /// <summary>Bit 18 (0x0000000000040000)</summary>        Bit18 = Bit17 << 1,         /// <summary>Bit 19 (0x0000000000080000)</summary>        Bit19 = Bit18 << 1,         /// <summary>Bit 20 (0x0000000000100000)</summary>        Bit20 = Bit19 << 1,         /// <summary>Bit 21 (0x0000000000200000)</summary>        Bit21 = Bit20 << 1,         /// <summary>Bit 22 (0x0000000000400000)</summary>        Bit22 = Bit21 << 1,         /// <summary>Bit 23 (0x0000000000800000)</summary>        Bit23 = Bit22 << 1,         // Byte 3, Bits 24-31         /// <summary>Bit 24 (0x0000000001000000)</summary>        Bit24 = Bit23 << 1,         /// <summary>Bit 25 (0x0000000002000000)</summary>        Bit25 = Bit24 << 1,         /// <summary>Bit 26 (0x0000000004000000)</summary>        Bit26 = Bit25 << 1,         /// <summary>Bit 27 (0x0000000008000000)</summary>        Bit27 = Bit26 << 1,         /// <summary>Bit 28 (0x0000000010000000)</summary>        Bit28 = Bit27 << 1,         /// <summary>Bit 29 (0x0000000020000000)</summary>        Bit29 = Bit28 << 1,         /// <summary>Bit 30 (0x0000000040000000)</summary>        Bit30 = Bit29 << 1,         /// <summary>Bit 31 (0x0000000080000000)</summary>        Bit31 = Bit30 << 1,         // Byte 4, Bits 32-39         /// <summary>Bit 32 (0x0000000100000000)</summary>        Bit32 = Bit31 << 1,         /// <summary>Bit 33 (0x0000000200000000)</summary>        Bit33 = Bit32 << 1,         /// <summary>Bit 34 (0x0000000400000000)</summary>        Bit34 = Bit33 << 1,         /// <summary>Bit 35 (0x0000000800000000)</summary>        Bit35 = Bit34 << 1,         /// <summary>Bit 36 (0x0000001000000000)</summary>        Bit36 = Bit35 << 1,         /// <summary>Bit 37 (0x0000002000000000)</summary>        Bit37 = Bit36 << 1,         /// <summary>Bit 38 (0x0000004000000000)</summary>        Bit38 = Bit37 << 1,         /// <summary>Bit 39 (0x0000008000000000)</summary>        Bit39 = Bit38 << 1,         // Byte 5, Bits 40-47         /// <summary>Bit 40 (0x0000010000000000)</summary>        Bit40 = Bit39 << 1,         /// <summary>Bit 41 (0x0000020000000000)</summary>        Bit41 = Bit40 << 1,         /// <summary>Bit 42 (0x0000040000000000)</summary>        Bit42 = Bit41 << 1,         /// <summary>Bit 43 (0x0000080000000000)</summary>        Bit43 = Bit42 << 1,         /// <summary>Bit 44 (0x0000100000000000)</summary>        Bit44 = Bit43 << 1,         /// <summary>Bit 45 (0x0000200000000000)</summary>        Bit45 = Bit44 << 1,         /// <summary>Bit 46 (0x0000400000000000)</summary>        Bit46 = Bit45 << 1,         /// <summary>Bit 47 (0x0000800000000000)</summary>        Bit47 = Bit46 << 1,         // Byte 6, Bits 48-55         /// <summary>Bit 48 (0x0001000000000000)</summary>        Bit48 = Bit47 << 1,         /// <summary>Bit 49 (0x0002000000000000)</summary>        Bit49 = Bit48 << 1,         /// <summary>Bit 50 (0x0004000000000000)</summary>        Bit50 = Bit49 << 1,         /// <summary>Bit 51 (0x0008000000000000)</summary>        Bit51 = Bit50 << 1,         /// <summary>Bit 52 (0x0010000000000000)</summary>        Bit52 = Bit51 << 1,         /// <summary>Bit 53 (0x0020000000000000)</summary>        Bit53 = Bit52 << 1,         /// <summary>Bit 54 (0x0040000000000000)</summary>        Bit54 = Bit53 << 1,         /// <summary>Bit 55 (0x0080000000000000)</summary>        Bit55 = Bit54 << 1,         // Byte 7, Bits 56-63         /// <summary>Bit 56 (0x0100000000000000)</summary>        Bit56 = Bit55 << 1,         /// <summary>Bit 57 (0x0200000000000000)</summary>        Bit57 = Bit56 << 1,         /// <summary>Bit 58 (0x0400000000000000)</summary>        Bit58 = Bit57 << 1,         /// <summary>Bit 59 (0x0800000000000000)</summary>        Bit59 = Bit58 << 1,         /// <summary>Bit 60 (0x1000000000000000)</summary>        Bit60 = Bit59 << 1,         /// <summary>Bit 61 (0x2000000000000000)</summary>        Bit61 = Bit60 << 1,         /// <summary>Bit 62 (0x4000000000000000)</summary>        Bit62 = Bit61 << 1,         /// <summary>Bit 63 (0x8000000000000000)</summary>        Bit63 = Bit62 << 1    }     #endregion     /// <summary>    /// Defines extension methods related to bit operations.    /// </summary>    public static class BitExtensions    {        /// <summary>        /// Gets the bit value for the specified bit index (0 - 63).        /// </summary>        /// <param name="bit">Bit index (0 - 63)</param>        /// <returns>Value of the specified <paramref name="bit"/>.</returns>        /// <exception cref="ArgumentOutOfRangeException">Parameter must be between 0 and 63.</exception>        [CLSCompliant(false)]        public static Bits BitVal(int bit)        {            switch (bit)            {                #region [ Bit Cases (0 - 63) ]                 case 00: return Bits.Bit00;                case 01: return Bits.Bit01;                case 02: return Bits.Bit02;                case 03: return Bits.Bit03;                case 04: return Bits.Bit04;                case 05: return Bits.Bit05;                case 06: return Bits.Bit06;                case 07: return Bits.Bit07;                case 08: return Bits.Bit08;                case 09: return Bits.Bit09;                case 10: return Bits.Bit10;                case 11: return Bits.Bit11;                case 12: return Bits.Bit12;                case 13: return Bits.Bit13;                case 14: return Bits.Bit14;                case 15: return Bits.Bit15;                case 16: return Bits.Bit16;                case 17: return Bits.Bit17;                case 18: return Bits.Bit18;                case 19: return Bits.Bit19;                case 20: return Bits.Bit20;                case 21: return Bits.Bit21;                case 22: return Bits.Bit22;                case 23: return Bits.Bit23;                case 24: return Bits.Bit24;                case 25: return Bits.Bit25;                case 26: return Bits.Bit26;                case 27: return Bits.Bit27;                case 28: return Bits.Bit28;                case 29: return Bits.Bit29;                case 30: return Bits.Bit30;                case 31: return Bits.Bit31;                case 32: return Bits.Bit32;                case 33: return Bits.Bit33;                case 34: return Bits.Bit34;                case 35: return Bits.Bit35;                case 36: return Bits.Bit36;                case 37: return Bits.Bit37;                case 38: return Bits.Bit38;                case 39: return Bits.Bit39;                case 40: return Bits.Bit40;                case 41: return Bits.Bit41;                case 42: return Bits.Bit42;                case 43: return Bits.Bit43;                case 44: return Bits.Bit44;                case 45: return Bits.Bit45;                case 46: return Bits.Bit46;                case 47: return Bits.Bit47;                case 48: return Bits.Bit48;                case 49: return Bits.Bit49;                case 50: return Bits.Bit50;                case 51: return Bits.Bit51;                case 52: return Bits.Bit52;                case 53: return Bits.Bit53;                case 54: return Bits.Bit54;                case 55: return Bits.Bit55;                case 56: return Bits.Bit56;                case 57: return Bits.Bit57;                case 58: return Bits.Bit58;                case 59: return Bits.Bit59;                case 60: return Bits.Bit60;                case 61: return Bits.Bit61;                case 62: return Bits.Bit62;                case 63: return Bits.Bit63;                 #endregion                 default:                    throw new ArgumentOutOfRangeException("bit", "Parameter must be between 0 and 63");            }        }         #region [ SetBits Extensions ]         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits"><see cref="Bits"/> to set.</param>        /// <returns><see cref="sbyte"/> value with specified <paramref name="bits"/> set.</returns>        [CLSCompliant(false)]        public static sbyte SetBits(this sbyte source, Bits bits)        {            checked            {                return SetBits(source, (sbyte)bits);            }        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits">Bit-mask of the bits to set.</param>        /// <returns><see cref="sbyte"/> value with specified <paramref name="bits"/> set.</returns>        [CLSCompliant(false)]        public static sbyte SetBits(this sbyte source, sbyte bits)        {            return ((sbyte)(source | bits));        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits"><see cref="Bits"/> to set.</param>        /// <returns><see cref="byte"/> value with specified <paramref name="bits"/> set.</returns>        [CLSCompliant(false)]        public static byte SetBits(this byte source, Bits bits)        {            checked            {                return SetBits(source, (byte)bits);            }        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits">Bit-mask of the bits to set.</param>        /// <returns><see cref="byte"/> value with specified <paramref name="bits"/> set.</returns>        public static byte SetBits(this byte source, byte bits)        {            return ((byte)(source | bits));        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits"><see cref="Bits"/> to set.</param>        /// <returns><see cref="short"/> value with specified <paramref name="bits"/> set.</returns>        [CLSCompliant(false)]        public static short SetBits(this short source, Bits bits)        {            checked            {                return SetBits(source, (short)bits);            }        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits">Bit-mask of the bits to set.</param>        /// <returns><see cref="short"/> value with specified <paramref name="bits"/> set.</returns>        public static short SetBits(this short source, short bits)        {            return ((short)(source | bits));        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits"><see cref="Bits"/> to set.</param>        /// <returns><see cref="ushort"/> value with specified <paramref name="bits"/> set.</returns>        [CLSCompliant(false)]        public static ushort SetBits(this ushort source, Bits bits)        {            checked            {                return SetBits(source, (ushort)bits);            }        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits">Bit-mask of the bits to set.</param>        /// <returns><see cref="ushort"/> value with specified <paramref name="bits"/> set.</returns>        [CLSCompliant(false)]        public static ushort SetBits(this ushort source, ushort bits)        {            return ((ushort)(source | bits));        }         /// <summary>        /// Returns value with specified <paramref name="bits"/> set.        /// </summary>        /// <param name="source">Value source.</param>        /// <param name="bits"><see cref="Bits"/> to set.</param>        /// <returns><see cref="Int24"/> value with specified <paramref name="bits"/> set.</returns>        [CLSCompliant(false)]        public static Int24 SetBits(this Int24 source, Bits bits)        {            checked            {                return SetBits(source, (Int24)bits);

Lines 1–500 of 2894.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t