Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 替换整数中的字节_C#_Bit Manipulation - Fatal编程技术网

C# 替换整数中的字节

C# 替换整数中的字节,c#,bit-manipulation,C#,Bit Manipulation,整数由4个字节组成。如何用新字节替换这4个字节中的一个。换句话说,我正在寻找一种方法: int ReplaceByte(int index, int value, byte replaceByte) { // implementation } 例如,如果我有值FFFFFFFF(-1),并且我想用0A(10)替换字节0,那么我将调用该方法,如下所示: ReplaceByte(0,-1,10) 我希望该方法返回我FFFFFF0A 我是否必须将int转换为字节数组,然后替换我想要的字节,然

整数由4个字节组成。如何用新字节替换这4个字节中的一个。换句话说,我正在寻找一种方法:

int ReplaceByte(int index, int value, byte replaceByte)
{
     // implementation
}
例如,如果我有值
FFFFFFFF
(-1),并且我想用
0A
(10)替换字节
0
,那么我将调用该方法,如下所示:

ReplaceByte(0,-1,10)

我希望该方法返回我
FFFFFF0A

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

编辑(结果) 感谢您的回答,我比较了各种方法:

结果如下:

注意,我的实现是最慢的

代码如下:

    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 );
    }
static void Main(字符串[]args)
{
字节[]随机字节=新字节[1024*1024*512];
Random r=新的Random();
r、 下个字节(随机字节);
Int64总和;
var now=DateTime.now;
控制台写入线(“测试1”);
总和=0;
现在=日期时间。现在;
foreach(var bt,以随机字节为单位)
{
sum+=ReplaceByte1(1,-1,bt);
}
WriteLine(“测试1在{0}秒\t hash={1}\n”内完成,(DateTime.Now-Now).TotalSeconds,sum);
控制台写入线(“测试2”);
总和=0;
现在=日期时间。现在;
foreach(var bt,以随机字节为单位)
{
sum+=ReplaceByte2(1,-1,bt);
}
WriteLine(“测试2在{0}秒\t hash={1}\n”内完成,(DateTime.Now-Now).TotalSeconds,sum);
控制台写入线(“测试3”);
总和=0;
现在=日期时间。现在;
foreach(var bt,以随机字节为单位)
{
sum+=ReplaceByte3(1,-1,bt);
}
WriteLine(“测试3在{0}秒\t hash={1}\n”内完成,(DateTime.Now-Now).TotalSeconds,sum);
Console.Read();
}
//测试1
静态int ReplaceByte1(int索引、int值、byte replaceByte)
{

返回(value&~(0xFFNo,没有字节数组。这实际上非常简单

未测试:

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

return(value&~(0xFF您可以简单地使用一些位算术:

// 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);
//为了使replaceByte“就位”,应该移动多少位
var shiftBits=8*指数;
//按位和该值清除应成为replaceByte的位

变量掩码=~(0xff好的,但是为什么
var
而不是
int
?它甚至不保存任何字母。@harold:只是习惯。我现在几乎从不按名称声明具体类型。奇怪的是,我没想到我的代码和Jon的代码在性能上有这么大的差异。在输入之前,你能尝试使用Stopwatch类和使用这些方法至少一次吗定时循环,而不是在同一阵列上多次使用大型阵列循环?