#define COPY_BIT(x, i, y, j) (y) = ((y) & ~(1 << (j))) | ((((x) >> (i)) & 1) << (j))
Something like that...? Can probably be optimized better.
Hwo to read bit and set same bit in different byte
What patrrr wrote seems correct. I would do it with a function, though:
The fact that you need to use some funky name for the type and that you are going for lots of generality is only confusing matters, so start without that.
unsigned copy_bit(unsigned x, int i, unsigned y, int j) {
y &= ~(1u << j); // Clear y's j-th bit
y |= ((x >> i) & 1u) << j; // Copy x's i-th bit into position j
return y;
}
The fact that you need to use some funky name for the type and that you are going for lots of generality is only confusing matters, so start without that.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement