替换int中的byte

时间:2012-10-31 15:21:33

标签: c# bit-manipulation

int由4个字节组成。我怎么能用一个新字节替换这4个字节中的一个。换句话说,我正在寻找一种方法:

int ReplaceByte(int index, int value, byte replaceByte)
{
     // implementation
}

例如,如果我有值FFFFFFFF( - 1)并且我想用0(10)替换字节0A,那么我将该方法称为:< / p>

ReplaceByte(0,-1,10)

我希望该方法可以返回FFFFFF0A

我是否必须将int转换为字节数组然后替换我想要的字节然后转换回int?我正在寻找一种有效的方法。我们正在创建一个类似于程序的调试程序,它连接到目标(板),我们会非常频繁地更新这些值。

编辑(结果)

感谢您的回答,我对方法进行了比较:

结果如下:

enter image description here

请注意我的实施速度最慢!

以下是代码:

    static void Main ( string[ ] args )
    {
        byte[ ] randomBytes = new byte[ 1024 * 1024 * 512 ]; 

        Random r = new Random( );
        r.NextBytes( randomBytes );

        Int64 sum;
        var now = DateTime.Now;

        Console.WriteLine( "Test 1" );
        sum = 0;
        now = DateTime.Now;
        foreach ( var bt in randomBytes )
        {
            sum += ReplaceByte1( 1 , -1 , bt );
        }

        Console.WriteLine( "Test 1 finished in {0} seconds \t hash = {1} \n" , ( DateTime.Now - now ).TotalSeconds, sum );

        Console.WriteLine( "Test 2" );
        sum = 0;
        now = DateTime.Now;
        foreach ( var bt in randomBytes )
        {
            sum += ReplaceByte2( 1 , -1 , bt );
        }

        Console.WriteLine( "Test 2 finished in {0} seconds \t hash = {1} \n" , ( DateTime.Now - now ).TotalSeconds,  sum );


        Console.WriteLine( "Test 3" );
        sum = 0;
        now = DateTime.Now;
        foreach ( var bt in randomBytes )
        {
            sum += ReplaceByte3( 1 , -1 , bt );
        }

        Console.WriteLine( "Test 3 finished in {0} seconds \t hash = {1} \n" , ( DateTime.Now - now ).TotalSeconds , sum );

        Console.Read( );            
    }

    // test 1
    static int ReplaceByte1 ( int index , int value , byte replaceByte )
    {
        return ( value & ~( 0xFF << ( index * 8 ) ) ) | ( replaceByte << ( index * 8 ) );
    }

    // test 2
    static int ReplaceByte2 ( int index , int value , byte replaceByte )
    {
        // how many bits you should shift replaceByte to bring it "in position"
        var shiftBits = 8 * index;

        // bitwise AND this with value to clear the bits that should become replaceByte
        var mask = ~( 0xff << shiftBits );

        // clear those bits and then set them to whatever replaceByte is
        return value & mask | ( replaceByte << shiftBits );
    }

    // test 3
    static int ReplaceByte3 ( int index , int value , byte replaceByte )
    {
        var bytes = BitConverter.GetBytes( value );
        bytes[ index ] = replaceByte;

        return BitConverter.ToInt32( bytes , 0 );
    }

2 个答案:

答案 0 :(得分:6)

不,没有字节数组。这其实很简单。

未经测试:

int ReplaceByte(int index, int value, byte replaceByte)
{
    return (value & ~(0xFF << (index * 8))) | (replaceByte << (index * 8));
}

首先清除指定索引处的空间,然后将新值放入该空间。

答案 1 :(得分:5)

您可以简单地使用一些按位算术:

// how many bits you should shift replaceByte to bring it "in position"
var shiftBits = 8 * index;

// bitwise AND this with value to clear the bits that should become replaceByte
var mask = ~(0xff << shiftBits);

// clear those bits and then set them to whatever replaceByte is
return value & mask | (replaceByte << shiftBits);