0
votes
$\begingroup$

I have three 32 bit integers a,b,c. I want to make 10th bit of a=(23 rd bit of b) xor (4th bit of c) without disturbing other bits of a. How can I do this in C programming language? a can be zero also. In that case I consider a= 00...0, 32 zeros.

$\endgroup$
4
  • 3
    $\begingroup$ I'm voting to close this question as off-topic because it's really a question for Stack Overflow. $\endgroup$
    – Bill Barth
    Commented Apr 13, 2015 at 15:19
  • $\begingroup$ stackoverflow, per my understand, is for when the OP has code the needs to be debugged $\endgroup$ Commented Apr 13, 2015 at 15:25
  • $\begingroup$ @user3629249: That is simply not true and the question is off-topic for SciComp.StackExchange.com. See this highly-rated question and its answers on StackOverflow: How do you set, clear and toggle a single bit in C/C++? $\endgroup$
    – horchler
    Commented Apr 13, 2015 at 19:17
  • $\begingroup$ I agree with horchler. Stack Exchange policy is not to migrate questions that already have accepted answers, even if the question is off-topic. Therefore, I will close the question and add a post notification to explain that this question is off-topic. $\endgroup$ Commented Apr 13, 2015 at 22:51

1 Answer 1

1
vote
$\begingroup$
// 10th bit of a=(23 rd bit of b) xor (4th bit of c)

if( a )
{ // skip if 'a' already all 0's
    int tempB = (b>>23)&0x01; // extract bit 23 and place on bit 0
    int tempC = (c>>4)&0x01;  // extract bit 4 and place in bit 0
    int tempXOR = (tempB^tempC)&0x01;// XOR the bits

    a &= ~(1<<10);        // clear bit 10
    a |= (tempXOR<<10);   // place result of XOR into bit 10 of a
}
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.