byte into another byte

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.

To make it more clearly:

byte copyfrom[] = {0, 1010, 0};
byte pastein[] = {10010101, 01010111, 01100000};

the second byte of copyfrom shall be copied to the third byte of pastein on index 4.
So it looks like this at the end:

byte pastein[] = {10010101, 01010111, 01101010};

PS: I'm a very big beginner in programming :sweat_smile: and please excuse my bad Englisch.

  byte copyfrom[] = {0b0, 0b1010, 0b0};
  byte pastein[] = {0b10010101, 0b01010111, 0b01100000};

  pastein[2] = (pastein[2] & 0b11110000) | (copyfrom[1] & 0b1111);

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

And there may well be a neater way to do it

...R

Four bits is called a "nybble".

What you asked has been illustrated. What happens if the pastein index is 2?

First of all thank you for your really quick help!
Slowly but constantly I get behind the code :smiley:
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. :confused:

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)