Skip to content

Instantly share code, notes, and snippets.

@AcidLeroy
Created May 18, 2019 00:17
Show Gist options
  • Save AcidLeroy/36d84bdfe736b81377ddc8d9472da94e to your computer and use it in GitHub Desktop.
Save AcidLeroy/36d84bdfe736b81377ddc8d9472da94e to your computer and use it in GitHub Desktop.
Bit copy function
#include <stdlib.h>
#include <stdio.h>
int copy_bit(int src, int dst, int pos)
{
int shifted = (src >> pos) & 0b1;
if (shifted != 0)
{
dst |= 1UL << pos;
return dst;
}
else
{
dst &= ~(1UL << pos);
return dst;
}
}
#ifndef RunTests
int main()
{
printf("Actual %d: expected is 4\n", copy_bit(7, 12, 3));
printf("Actual %d: expected is 14\n", copy_bit(7, 12, 1));
printf("Actual %d: expected is 13\n", copy_bit(7, 12, 0));
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment