Swapping bits at a given point between two bytes
问题 Let's say I have these two numbers: x = 0xB7 y = 0xD9 Their binary representations are: x = 1011 0111 y = 1101 1001 Now I want to crossover (GA) at a given point, say from position 4 onwards. The expected result should be: x = 1011 1001 y = 1101 0111 Bitwise, how can I achieve this? 回答1: I would just use bitwise operators: t = (x & 0x0f) x = (x & 0xf0) | (y & 0x0f) y = (y & 0xf0) | t That would work for that specific case. In order to make it more adaptable, I'd put it in a function,