I have found this answer to a similar question where one can extract a byte from a larger type. What I would like to know is what would be the reverse of this procedure by the use of an index value?
To extract a byte from a 32bit type user Pete Wilson gave this:
int a = (the_int >> 24) & 0xff; // high-order (leftmost) byte: bits 24-31 int b = (the_int >> 16) & 0xff; // next byte, counting from left: bits 16-23 int c = (the_int >> 8) & 0xff; // next byte, bits 8-15 int d = the_int & 0xff; // low-order byte: bits 0-7
I would like to do the opposite with an index value:
// assume int = 32bit or 4 bytes and unsigned char = 8bit or 1 byte
void insertByte( unsigned char a, unsigned int& value, unsigned idx ) {
// How to take byte and insert it into the byte position
// of value at idx where idx is [0-3] from the right
}
Here's an example by value:
unsigned char a = 0x9D;
unsigned int value = 0;
insertByte( a, value, 0 ); // value = 0x0000009D;
insertByte( a, value, 1 ); // value = 0x00009D00;
insertByte( a, value, 2 ); // value = 0x009D0000;
insertByte( a, value, 3 ); // value = 0x9D000000;
// insertByte( a, value, >3 ); // invalid index
Edit
User jamit made a good point with his question in the comments. I think at this moment because this will be the behavior of a constructor to a class template; I want to insert and replace, not insert and shift.
Example:
unsigned int value = 0x01234567;
unsigned char a = 0x9D;
insertByte( a, value, 2 );
// value = 0x019D4567