C#获取并设置整数的高位字

获取和设置整数的高阶部分的有效或语法简单方法是什么?

它与C / C ++中的相同:

// get the high order 16 bits int high = 0x12345678 >> 16; // high = 0x1234 // set the high order 16 bits high = (high & 0x0000FFFF) + (0x5678 << 16); // high = 0x56781234 

编辑:因为我心情很好,所以你去吧。 请记住,不可变类型是不可变的! 'set'函数需要分配给某些东西。

 public static class ExtensionMethods { public int LowWord(this int number) { return number & 0x0000FFFF; } public int LowWord(this int number, int newValue) { return (number & 0xFFFF0000) + (newValue & 0x0000FFFF); } public int HighWord(this int number) { return number & 0xFFFF0000; } public int HighWord(this int number, int newValue) { return (number & 0x0000FFFF) + (newValue << 16); } } 

编辑2:再想一想,如果你真的需要这样做而且不想在任何地方使用语法,请使用Michael的解决方案。 向他展示新的东西+1。

组:

 MessageBox.Show( string.Format("{0:X}", 8 | 0x12340000) ); 

得到:

 MessageBox.Show( string.Format("{0:X}", 0xDEADBEEF >> 16) ); 

[编辑]

C#非常支持共享相同内存位置和位结构的变量

来源: http : //msdn.microsoft.com/en-us/library/acxa5b99(VS.80).aspx

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Bitwise { public partial class Form1 : Form { [StructLayout(LayoutKind.Explicit)] struct TestUnion { [FieldOffset(0)] public uint Number; [FieldOffset(0)] public ushort Low; [FieldOffset(2)] public ushort High; } public Form1() { InitializeComponent(); var x = new TestUnion { Number = 0xDEADBEEF }; MessageBox.Show(string.Format("{0:X} {1:X} {2:X}", x.Number, x.High, x.Low)); x.High = 0x1234; MessageBox.Show(string.Format("{0:X} {1:X} {2:X}", x.Number, x.High, x.Low)); x.Low = 0x5678; MessageBox.Show(string.Format("{0:X} {1:X} {2:X}", x.Number, x.High, } } } 

注意:由于C#没有函数,因此上述方法比将变量传递给方法/扩展方法更具性能

我想你不想要Hiword / Hibyte或LoWord / Lobyte进行计算,如果System.Int32从地址100开始(因此它占用地址100到103),你想要作为LoWord从两个字节开始地址100和101和Hiword是地址102和103。

这可以使用BitConverter类实现。 这个类对这些位没有任何作用,它只使用地址来返回请求的值。

由于int / long等类型的大小每个平台不同,而WORD和DWORD有点令人困惑,我使用系统类型System.Int16 / Int32 / Int64。 猜测System.Int32中的位数没有任何问题。

使用BitConverter,您可以将任何整数转换为从该位置开始的字节数组,并将适当长度的字节数组转换为相应的整数。 无需计算,也不会改变位数,

假设您有一个System.Int32 X(旧的DWORD)

 LOWORD: System.Int16 y = BitConverter.ToInt16(BitConverter.GetBytes(x), 0); HIWORD: System.Int16 y = BitConverter.ToInt16(BitConverter.GetBytes(x), 2); 

好处是它适用于所有长度,你不必将像LOBYTE和HIORD这样的函数组合起来得到第三个字节:

 HIByte(Hiword(x)) will be like: BitConverter.GetBytes(x)[3] 

另一种选择

  public class Macro { public static short MAKEWORD(byte a, byte b) { return ((short)(((byte)(a & 0xff)) | ((short)((byte)(b & 0xff))) << 8)); } public static byte LOBYTE(short a) { return ((byte)(a & 0xff)); } public static byte HIBYTE(short a) { return ((byte)(a >> 8)); } public static int MAKELONG(short a, short b) { return (((int)(a & 0xffff)) | (((int)(b & 0xffff)) << 16)); } public static short HIWORD(int a) { return ((short)(a >> 16)); } public static short LOWORD(int a) { return ((short)(a & 0xffff)); } } 

我用这两个function……

  public static int GetHighint(long intValue) { return Convert.ToInt32(intValue >> 32); } public static int GetLowint(long intValue) { long tmp = intValue << 32; return Convert.ToInt32(tmp >> 32); }