Solution:
typedef unsigned char *byte_pointer;
static int int_of_bit (byte_pointer x, int loc)
{
return(x[loc] << loc*8);
}
static int replace_byte(unsigned int a, int loc, unsigned int b)
unsigned int a_loc = int_of_bit((byte_pointer) &a , loc);
unsigned int b_loc = (b << loc*8);
a -= a_loc;
a += b_loc;
return a;
}
Explanation:
This takes two ints in hex format, one with 8 bits (0x00000000) and one with 2 bits (0x00) then places the 2 bit hex into the 8 bit at a given location.
EX: replace_byte(0x00000000, 1, 0xFF) return 0x0000FF00
First thing first, it looks like you have some mixup:
" one with 8 bits (0x00000000) and one with 2 bits (0x00)*- what you mean is nibble not bits. Each hex character (0-9, A-F) represent 4 bits (16 possible combination), and is called a "nibble".
0x0000000 is 4 bytes. 0x00 is 2 bytes.
Next, you say "takes two ints in hex format" - your function takes two ints in any format. You're just choosing tp express them in hexidecimal. C++ doesn't care if you specify numbers in hex, decimal, octal, binary, etc.