Jump to content

  • Log In with Google      Sign In   
  • Create Account

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.

  • You cannot reply to this topic
22 replies to this topic

#21 patrrr   Members   -  Reputation: 1004

Like
1Likes
Like

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.


Sponsor:

#22 Álvaro   Crossbones+   -  Reputation: 13310

Like
1Likes
Like

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.

#23 Misery   Members   -  Reputation: 317

Like
0Likes
Like

Posted 18 October 2012 - 09:21 AM

@alvaro & @patrrr: Thanks a lot Guys. That was exactly the thing I was looking for. Posted Image
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.



PARTNERS