Well, here is the simple function you need:
#include <iostream>
void moveAByte( unsigned long int& destinationLong, const int& sourceInt,
const std::size_t destByteOffset, const std::size_t sourceByteOffset )
{
uint8_t* const firstByteAddressOfLong { reinterpret_cast<uint8_t* const>( &destinationLong ) };
const uint8_t* const firstByteAddressOfInt { reinterpret_cast<const uint8_t* const>( &sourceInt ) };
*( firstByteAddressOfLong + destByteOffset ) = *( firstByteAddressOfInt + sourceByteOffset );
}
int main( )
{
unsigned long int v1 { 0x12'34'56'78 }; // a sample number
int v2 { 0x19'ab'cd'ef }; // another sample number
const std::size_t destByteOffset { 3 }; // Here choose between 0, 1, 2 ,3
const std::size_t sourceByteOffset { 0 }; // the same here
std::cout << '\n';
std::cout << "Before copying the byte " << sourceByteOffset << " of v2 to byte " << destByteOffset << " of v1" << "\n\n"
<< std::showbase << std::hex << "v1 address: " << &v1 << " --> " << "v1 value: " << v1 << '\n'
<< "v2 address: " << &v2 << " --> " << "v2 value: " << v2 << '\n';
std::cout << std::dec << '\n' << "------------------------------------------------------" << "\n\n";
moveAByte( v1, v2, destByteOffset, sourceByteOffset ); // source byte located in v2 is copied to destination byte located in v1
std::cout << "After copying the byte " << sourceByteOffset << " of v2 to byte " << destByteOffset << " of v1" << "\n\n"
<< std::showbase << std::hex << "v1 address: " << &v1 << " --> " << "v1 value: " << v1 << '\n'
<< "v2 address: " << &v2 << " --> " << "v2 value: " << v2 << '\n';
return 0;
}
Sample output:
Before copying the byte 0 of v2 to byte 3 of v1
v1 address: 0x8504fffc78 --> v1 value: 0x12345678
v2 address: 0x8504fffc7c --> v2 value: 0x19abcdef
------------------------------------------------------
After copying the byte 0 of v2 to byte 3 of v1
v1 address: 0x8504fffc78 --> v1 value: 0xef345678
v2 address: 0x8504fffc7c --> v2 value: 0x19abcdef
As can be seen, in this example, the least significant byte of v2 which is ef is copied to the most significant byte of v1 which was 12 but now it's ef too.
Using the function moveAByte you can easily copy any byte from v1 to any position in v2.
Note: This code only works on little-endian machines and not on big-endian ones.
Note: On my platform (Windows), long is 4 bytes. On macOS and Linux it's 8 bytes. So don't forget to take this into account if you're on macOS or Linux.