Hi, for a project I need to copy a byte from a byte-array into a specific location of another byte from a byte-array. I searched but I have only found the Array.CopyTo Method of the .NET Framework which shouldn't work on the Arduino.
If you want to replace the low 4 bits in byteB with the low 4 bits from byteA ...
byteA = 0b01010101;
byteB = 0b11111111;
byteTemp = byteA & 0b00001111; // converts the high 4 bits to 0 so they won't be carried over
byteB = byteB & 0b11110000; // clears the bottom 4 bits of byteB
byteB = byteB | byteTemp; // logical OR of the bits in the two bytes
// byteB should now be 0b11110101
First of all thank you for your really quick help!
Slowly but constantly I get behind the code
But the index needs to be variable. Maybe you could make it variable by replacing the used bytes with variables/arrays which use also bytes for the wanted index (means for index 2 0b00011111 and 0b11100000) but it's not the most elegant solution.
Unless you are deeply concerned with efficiency, you should approach this by creating bitwise functions to address the data. Then the logic can be translated more or less directly from your requirement.
Example,
copyOneBit(dest, source, offset)
which makes it easy to code:
copyBits(dest, source, offset, numberOfBits)