• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Hwo to read bit and set same bit in different byte

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

22 replies to this topic

### #21patrrr  Members

Posted 17 October 2012 - 05:12 AM

#define COPY_BIT(x, i, y, j) (y) = ((y) & ~(1 << (j))) | ((((x) >> (i)) & 1) << (j))

Something like that...? Can probably be optimized better.

Edited by patrrr, 17 October 2012 - 06:33 AM.

### #22Álvaro  Members

Posted 17 October 2012 - 06:20 AM

What patrrr wrote seems correct. I would do it with a function, though:
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.

### #23Misery  Members

Posted 18 October 2012 - 09:21 AM

@alvaro & @patrrr: Thanks a lot Guys. That was exactly the thing I was looking for.
alvaro: at the moment I cannot even count how many times you have halped me. Special thanks and huge respect to your skills!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.