Replace byte in int

A int consists of 4 bytes. How can I replace one of these 4 bytes with a new byte. In other words, I am looking for a way:

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

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

ReplaceByte(0,-1,10)

and I will like this method to return me FFFFFF0A

Do I need to convert an int to an array of bytes, and then replace the byte I want, and then convert back to int? I am looking for an effective way to do this. We create a debugger similar to a program that connects to the target (board), and we often update these values.

Edit (Results)

Thanks to your answers, I compared the methods:

here are the results:

enter image description here

Please note that 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 ); } 
+4
source share
2 answers

No, there are no byte arrays. It is 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 into that space.

+6
source

You can just 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); 
+5
source

Source: https://habr.com/ru/post/1443256/


All Articles