Created
May 18, 2019 00:17
-
-
Save AcidLeroy/36d84bdfe736b81377ddc8d9472da94e to your computer and use it in GitHub Desktop.
Bit copy function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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