Created
January 19, 2017 15:24
-
-
Save Sreyas-Sreelal/9c92571f6c1ccc520a0c3c4db0ad9cc1 to your computer and use it in GitHub Desktop.
Demonstration of some practical uses of bit wise operators.
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
//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