set bit in specific position

find the bit of that position
int mask = 1 << pos;
value = src & mask;

clear the position of dst then set the bit to value
(dst & ~mask) | (value & mask);

#include 
#include 

int copy_bit(int src, int dst, int pos)
{
    int result = 0;
    int mask = 1 << pos;
    int value = 0;
    value = src & mask;
    return (dst & ~mask) | (value & mask);
}

#ifndef RunTests
int main()
{
    printf("%d", copy_bit(8, 12, 3));
}
#endif

發表留言