13.01.2017, 14:13
(
Последний раз редактировалось SyS; 19.01.2017 в 14:35.
)
Practical uses of Bit wise Operators
Gist Link
Many people asked me for what purpose we can use bitwise operators?Is it worth it?
So as to answer these questions im providing some snippets that i learned in high school and might explain the handy uses of bitwise operators Here are some practical uses of bitwise operators.
Gist Link
Many people asked me for what purpose we can use bitwise operators?Is it worth it?
So as to answer these questions im providing some snippets that i learned in high school and might explain the handy uses of bitwise operators Here are some practical uses of bitwise operators.
Swapping two variables without the use of 3rd one using XOR
PHP код:
swap(&a,&b)
{
a ^= b;
b ^= a;
a ^= b;
}
Checking two numbers have same signs using XOR
PHP код:
bool:IsSameSign(a,b)
{
return (!((a ^ b) < 0));
}
Checking is odd using AND
PHP код:
bool:IsOdd(a)
{
return ((a & 1) > 0);
}
Checking is even using AND
PHP код:
bool:IsEven(a)
{
return ((a & 1) == 0);
}
To Lower using OR and Left Shift
PHP код:
ToLower(a)
{
return (a |(1<<5));
}
To Upper using Not and AND (NAND) and Left Shift
PHP код:
ToUpper(a)
{
return (a &~(1<<5));
}