Skip to content

Instantly share code, notes, and snippets.

@Sreyas-Sreelal
Created January 19, 2017 15:24
Show Gist options
  • Save Sreyas-Sreelal/9c92571f6c1ccc520a0c3c4db0ad9cc1 to your computer and use it in GitHub Desktop.
Save Sreyas-Sreelal/9c92571f6c1ccc520a0c3c4db0ad9cc1 to your computer and use it in GitHub Desktop.
Demonstration of some practical uses of bit wise operators.
//Swapping two variables without the use of 3rd one using XOR
swap(&a,&b)
{
a ^= b;
b ^= a;
a ^= b;
}
//Checking two numbers have same signs using XOR
bool:IsSameSign(a,b)
{
return (!((a ^ b) < 0));
}
//Checking is odd using AND
bool:IsOdd(a)
{
return ((a & 1) > 0);
}
//Checking is even using AND
bool:IsEven(a)
{
return ((a & 1) == 0);
}
//To Lower using OR and Left Shift
ToLower(a)
{
return (a |(1<<5));
}
//To Upper using Not and AND (NAND) and Left Shift
ToUpper(a)
{
return (a &~(1<<5));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment