//****************************************************************************************************** // Word.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: // ---------------------------------------------------------------------------------------------------- // 04/10/2009 - James R. Carroll // Generated original version of source code. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 10/8/2012 - Danyelle Gilliam // Modified Header // //****************************************************************************************************** using System; namespace GSF { /// /// Represents functions and extensions related to 16-bit words, 32-bit double-words and 64-bit quad-words. /// [CLSCompliant(false)] public static class Word { /// /// Aligns word value on a 16-bit boundry. /// /// Word value to align. /// Word value aligned to next 16-bit boundry. public static short AlignWord(this short word) { return (short)(word + 1 - (word - 1) % 2); } /// /// Aligns word value on a 16-bit boundry. /// /// Word value to align. /// Word value aligned to next 16-bit boundry. public static ushort AlignWord(this ushort word) { return (ushort)(word + 1 - (word - 1) % 2); } /// /// Aligns double-word value on a 32-bit boundry. /// /// Double-word value to align. /// Double-word value aligned to next 32-bit boundry. public static int AlignDoubleWord(this int doubleWord) { return doubleWord + 3 - (doubleWord - 1) % 4; } /// /// Aligns double-word value on a 32-bit boundry. /// /// Double-word value to align. /// Double-word value aligned to next 32-bit boundry. public static uint AlignDoubleWord(this uint doubleWord) { return doubleWord + 3 - (doubleWord - 1) % 4; } /// /// Aligns quad-word value on a 64-bit boundry. /// /// Quad-word value to align. /// Quad-word value aligned to next 64-bit boundry. public static long AlignQuadWord(this long quadWord) { return quadWord + 7 - (quadWord - 1) % 8; } /// /// Aligns quad-word value on a 64-bit boundry. /// /// Quad-word value to align. /// Quad-word value aligned to next 64-bit boundry. public static ulong AlignQuadWord(this ulong quadWord) { return quadWord + 7 - (quadWord - 1) % 8; } /// /// Returns the high-nibble (high 4-bits) from a byte. /// /// Byte value. /// The high-nibble of the specified byte value. public static byte HighNibble(this byte value) { return (byte)((value & (byte)0xF0) >> 4); } /// /// Returns the high-byte from an unsigned word (UInt16). /// /// 2-byte, 16-bit unsigned integer value. /// The high-order byte of the specified 16-bit unsigned integer value. /// /// On little-endian architectures (e.g., Intel platforms), this will be the byte value whose in-memory representation /// is the same as the right-most, most-significant-byte of the integer value. /// public static byte HighByte(this ushort word) { return (byte)((word & (ushort)0xFF00) >> 8); } /// /// Returns the unsigned high-word (UInt16) from an unsigned double-word (UInt32). /// /// 4-byte, 32-bit unsigned integer value. /// The unsigned high-order word of the specified 32-bit unsigned integer value. /// /// On little-endian architectures (e.g., Intel platforms), this will be the word value /// whose in-memory representation is the same as the right-most, most-significant-word /// of the integer value. /// public static ushort HighWord(this uint doubleWord) { return (ushort)((doubleWord & (uint)0xFFFF0000) >> 16); } /// /// Returns the unsigned high-double-word (UInt32) from an unsigned quad-word (UInt64). /// /// 8-byte, 64-bit unsigned integer value. /// The high-order double-word of the specified 64-bit unsigned integer value. /// /// On little-endian architectures (e.g., Intel platforms), this will be the word value /// whose in-memory representation is the same as the right-most, most-significant-word /// of the integer value. /// public static uint HighDword(this ulong quadWord) { return (uint)((quadWord & (ulong)0xFFFFFFFF00000000) >> 32); } /// /// Returns the low-nibble (low 4-bits) from a byte. /// /// Byte value. /// The low-nibble of the specified byte value. public static byte LowNibble(this byte value) { return (byte)(value & (byte)0x0F); } /// /// Returns the low-byte from an unsigned word (UInt16). /// /// 2-byte, 16-bit unsigned integer value. /// The low-order byte of the specified 16-bit unsigned integer value. /// /// On little-endian architectures (e.g., Intel platforms), this will be the byte value /// whose in-memory representation is the same as the left-most, least-significant-byte /// of the integer value. /// public static byte LowByte(this ushort word) { return (byte)(word & (ushort)0x00FF); } /// /// Returns the unsigned low-word (UInt16) from an unsigned double-word (UInt32). /// /// 4-byte, 32-bit unsigned integer value. /// The unsigned low-order word of the specified 32-bit unsigned integer value. /// /// On little-endian architectures (e.g., Intel platforms), this will be the word value /// whose in-memory representation is the same as the left-most, least-significant-word /// of the integer value. /// public static ushort LowWord(this uint doubleWord) { return (ushort)(doubleWord & (uint)0x0000FFFF); } /// /// Returns the unsigned low-double-word (UInt32) from an unsigned quad-word (UInt64). /// /// 8-byte, 64-bit unsigned integer value. /// The low-order double-word of the specified 64-bit unsigned integer value. /// /// On little-endian architectures (e.g., Intel platforms), this will be the word value /// whose in-memory representation is the same as the left-most, least-significant-word /// of the integer value. /// public static uint LowDword(this ulong quadWord) { return (uint)(quadWord & (ulong)0x00000000FFFFFFFF); } /// /// Makes an unsigned word (UInt16) from two bytes. /// /// High byte. /// Low byte. /// An unsigned 16-bit word made from the two specified bytes. public static ushort MakeWord(byte high, byte low) { return (ushort)(low + ((ushort)high << 8)); } /// /// Makes an unsigned double-word (UInt32) from two unsigned words (UInt16). /// /// High word. /// Low word. /// An unsigned 32-bit double-word made from the two specified unsigned 16-bit words. public static uint MakeDword(ushort high, ushort low) { return (uint)(low + ((uint)high << 16)); } /// /// Makes an unsigned quad-word (UInt64) from two unsigned double-words (UInt32). /// /// High double-word. /// Low double-word. /// An unsigned 64-bit quad-word made from the two specified unsigned 32-bit double-words. public static ulong MakeQword(uint high, uint low) { return (ulong)(low + ((ulong)high << 32)); } } }