Last active
August 29, 2015 14:08
-
-
Save h2rashee/abf19bcdf940fb2893b8 to your computer and use it in GitHub Desktop.
Modifying bit positions of an integer
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 <stdio.h> | |
#define BIT(x) (0x1 << x) | |
int set(int val, int pos) | |
{ | |
return (val |= BIT(pos)); | |
} | |
int unset(int val, int pos) | |
{ | |
return (val &= ~BIT(pos)); | |
} | |
// Examples to test | |
int main() | |
{ | |
printf("%d\n", set(1, 3)); | |
printf("%d\n", set(5, 3)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment