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

时间:2009-09-17 00:42:46

标签: c#

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

5 个答案:

答案 0 :(得分:26)

uint debugValue = 0xDEADBEEF;

// get
var toExtractHigh = debugValue >> 16;
Console.WriteLine("{0:X}", toExtractHigh);

// set
uint toSetHigh = 0xABAD;
debugValue = debugValue & 0x0000FFFF | toSetHigh << 16;
// this would work too:
//     debugValue = debugValue & 0xFFFF | toSetHigh << 16;
Console.WriteLine("{0:X}", debugValue);

输出:

DEAD
ABADBEEF

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

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

using System;

using System.Runtime.InteropServices;


[StructLayout(LayoutKind.Explicit)]
struct TestUnion
{

    [FieldOffset(0)]
    public uint Number;

    [FieldOffset(0)]
    public ushort Low;

    [FieldOffset(2)]
    public ushort High;
}


class MainClass
{
    public static void Main(string[] args)
    {        
        var x = new TestUnion { Number = 0xABADF00D };
        Console.WriteLine("{0:X} {1:X} {2:X}", x.Number, x.High, x.Low);

        x.Low = 0xFACE;
        Console.WriteLine("{0:X} {1:X} {2:X}", x.Number, x.High, x.Low);

        x.High = 0xDEAD;
        Console.WriteLine("{0:X} {1:X} {2:X}", x.Number, x.High, x.Low);
    }
}

输出:

ABADF00D ABAD F00D
ABADFACE ABAD FACE
DEADFACE DEAD FACE

注意:由于C#没有像C中那样的功能,因此您可以使用联合方法加快速度。它比将变量传递给方法/扩展方法

更具性能

或者,如果您想在C#中编码C,请使用unsafe

unsafe
{
    uint value = 0xCAFEFEED;

    // x86 is using low-endian. 
    // So low order array number gets the low order of the value
    // And high order array number gets the high order of the value
    Console.WriteLine(
        "Get low order of {0:X}: {1:X}", 
        value, ((ushort *) &value)[0]);

    Console.WriteLine(
        "Get high order of {0:X}: {1:X}", 
        value, ((ushort*) &value)[1]);


    ((ushort*) &value)[1] = 0xABAD;
    Console.WriteLine("Set high order to ABAD: {0:X}", value);

    ((ushort*) &value)[0] = 0xFACE;
    Console.WriteLine("Set low order to FACE: {0:X}", value);
}

输出:

Get low order of CAFEFEED: FEED
Get high order of CAFEFEED: CAFE
Set high order to ABAD: ABADFEED
Set low order to FACE: ABADFACE

另一种unsafe方法:

unsafe
{
    uint value = 0xCAFEFEED;

    Console.WriteLine(
        "Get low order of {0:X}: {1:X}", 
        value, ((TestUnion*) &value)->Low);

    Console.WriteLine(
        "Get high order of {0:X}: {1:X}", 
        value, ((TestUnion*) &value)->High);


    ((TestUnion*) &value)->High = 0xABAD;
    Console.WriteLine("Set high order to ABAD: {0:X}", value);

    ((TestUnion*) &value)->Low = 0xFACE;
    Console.WriteLine("Set low order to FACE: {0:X}", value);
}

输出:

Get low order of CAFEFEED: FEED
Get high order of CAFEFEED: CAFE
Set high order to ABAD: ABADFEED
Set low order to FACE: ABADFACE

答案 1 :(得分:17)

与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:第二个想法,如果你真的需要这样做而且不想在任何地方使用语法,请使用迈克尔的解决方案。给他+1给我一些新的东西。

答案 2 :(得分:4)

我想你想要Hiword / Hibyte或LoWord / Lobyte时不需要计算, 如果System.Int32从地址100开始(因此它占用地址100到103),你想要一个从地址100和101开始的两个字节作为LoWord,而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]

答案 3 :(得分:1)

另一种选择

    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));
        }
    }

答案 4 :(得分:0)

我使用这两个功能...

    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);
    }