I have a 1 byte array in C, like
array[4 to 7] = 0x01020304 XOR array[0 to 3]
A way that's well-defined in the C standard is to
uint32_t. Call it
memcpy the relevant part of
Perform the XOR.
memcpy that back to
This approaches elegance if you wrap this up in a function. Note that the results of this operation will be endianness-sensitive so will vary from platform to platform.
Alternatively, if you put endianness concerns on the back-burner, you could split up
0x01020304 into 4 bytes and perform the operation byte by byte directly on
Other approaches include type punning through a
union or ditching the language entirely and using inline assembly (using the byte-by-byte approach). Note that making arbitrary conversions and casts in C is often a hair's breadth from rendering the behaviour of your code undefined, so be careful if you go down that path.