Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace byte in a int

A int is composed of 4 bytes. How could I replace one of those 4 bytes with a new byte. In other words I am looking for a method:

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

for example if I have the value FFFFFFFF (-1) and I will like to replace the byte 0 with 0A (10) then I will call the method as:

ReplaceByte(0,-1,10)

and I will like that method to return me FFFFFF0A

Do I have to convert the int to a byte array then replace the byte I want then convert back to an int? I am looking for an efficient way of doing this. We are creating a debugger like program that connects to a target (board) and we update these values very frequently.

Edit (results)

Thanks to your answers I compared the methods:

here are the results:

enter image description here

Note my implementation was the slowest!

Here is the code:

    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 );
    }
like image 599
Tono Nam Avatar asked Dec 20 '22 14:12

Tono Nam


2 Answers

No, no bytes arrays. This actually very simple.

Not tested:

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

First it clears the space where at the specified index, and then it puts the new value in that space.

like image 56
harold Avatar answered Jan 05 '23 06:01

harold


You can simply use some bitwise arithmetic:

// 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);
like image 34
Jon Avatar answered Jan 05 '23 06:01

Jon