Skip to content

Instantly share code, notes, and snippets.

@h2rashee
Last active August 29, 2015 14:08
Show Gist options
  • Save h2rashee/abf19bcdf940fb2893b8 to your computer and use it in GitHub Desktop.
Save h2rashee/abf19bcdf940fb2893b8 to your computer and use it in GitHub Desktop.
Modifying bit positions of an integer
#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