I need to do this millions of times as fast as possible. Say I have two lists of several short char
arrays:
"a b ", "a c ", "a x ", etc...
" w z", " w y", " q b"
Now I want to form combinations of one from each list. For example, "a b "
and " w z"
would become "awbz"
.
It seems like the most efficient way would be to store them as a 32-bit sequences:
"a b " --> 0x00620061
" w z" --> 0x7A007700
Now OR
them together to get
0x7A627761 --> "awbz"
My first thought is to use a union, but I know that this technically presents undefined behavior...writing to part of a union variable followed by reading a different type from the union.
union {
unsigned char[4] c;
unsigned int i;
};
My second thought would be to use casts to switch between int and char[]. Is there a way to safely do it this way?