Speed up Your Code Using Bitwise Operators :
x/2=x>>1
x/4=x>>2
x/8=x>>3
..................and so on
x*2=x<<1
x*3=(i<<1)+x
x*4=x<<2
x*5=(x<<2)+x
x*6=(x<<2)+(x<<1)
x*7=(x<<3)-x
x*8=x<<3
................and so on
x%2=x&1
x%4=x&3
x%8=x&7
..............and so on
Bitwise Operator :
The bitwise operators operate on numbers (always integers) as if they were sequences of binary bits (which, of course, internally to the computer they are). These operators will make the most sense, therefore, if we consider integers as represented in binary, octal, or hexadecimal (bases 2, 8, or 16), not decimal (base 10). Remember, you can use octal constants in C by prefixing them with an extra 0 (zero), and you can use hexadecimal constants by prefixing them with 0x (or 0X).
List of bitwise Operators :
& - bitwise and
| - bitwise or
^ - bitwise xor
~ - bitwise not
<< - bitwise shift left
>> - bitwise shift right
Bitwise AND ( & ) :
Bitwise AND operation between x and y :
x = 101010
& y = 011011
---------------------------
z = 001010
Bitwise OR ( | ) :
Bitwise OR operation between x and y :
x = 101010
| y = 011011
-----------------------
z = 111011
Bitwise XOR ( ^ ) :
Bitwise XOR operation between x and y :
x = 101010
^ y = 011011
-----------------------
z = 110001
Bitwise NOT ( ~ ) :
Bitwise NOT operation on x :
x= 101010
~x= 010101
Bitwise Left Shift ( << ) :
Bitwise left shift operation on x :
x = 0000 1011
x << 3 = 0101 1000
Bitwise Right Shift ( >> ) :
Bitwise right shift operation on x :
x = 0000 1010
x >> 1 = 0000 0101
Some examples of bit operations :
1.Check an integer is even or odd:
if ((x & 1) == 0) {
x is even
}
else {
x is odd
}
2. Test the n-th bit set :
if (x & (1<<n)) {
n-th bit is set
}
else {
n-th bit is not set
}
3.Set the n-th bit :
y= x | (1<<n )
4. Unset the n-th bit :
y = x & ~(1<<n)
5. Toggle the n-th bit :
y = x ^ (1<<n)
6.Turn off the rightmost 1-bit :
y = x & (x-1)
7.Isolate the rightmost 1-bit :
y = x & (-x)
8.Right propagate the rightmost 1-bit :
y = x | (x-1)
9.Isolate the rightmost 0-bit :
y = ~x & (x+1)
10. Turn on the rightmost 0-bit :
y = x | (x+1)
11. Find maximum :
r = x ^ ((x ^ y) & -(x < y)); // max(x, y)
12. Find Minimum :
r = y ^ ((x ^ y) & -(x < y)); // min(x, y)
13.Swap between two integer :
The bitwise operators operate on numbers (always integers) as if they were sequences of binary bits (which, of course, internally to the computer they are). These operators will make the most sense, therefore, if we consider integers as represented in binary, octal, or hexadecimal (bases 2, 8, or 16), not decimal (base 10). Remember, you can use octal constants in C by prefixing them with an extra 0 (zero), and you can use hexadecimal constants by prefixing them with 0x (or 0X).
List of bitwise Operators :
& - bitwise and
| - bitwise or
^ - bitwise xor
~ - bitwise not
<< - bitwise shift left
>> - bitwise shift right
Bitwise AND ( & ) :
Bitwise AND operation between x and y :
x = 101010
& y = 011011
---------------------------
z = 001010
Bitwise OR ( | ) :
Bitwise OR operation between x and y :
x = 101010
| y = 011011
-----------------------
z = 111011
Bitwise XOR ( ^ ) :
Bitwise XOR operation between x and y :
x = 101010
^ y = 011011
-----------------------
z = 110001
Bitwise NOT ( ~ ) :
Bitwise NOT operation on x :
x= 101010
~x= 010101
Bitwise Left Shift ( << ) :
Bitwise left shift operation on x :
x = 0000 1011
x << 3 = 0101 1000
Bitwise Right Shift ( >> ) :
Bitwise right shift operation on x :
x = 0000 1010
x >> 1 = 0000 0101
Some examples of bit operations :
1.Check an integer is even or odd:
if ((x & 1) == 0) {
x is even
}
else {
x is odd
}
2. Test the n-th bit set :
if (x & (1<<n)) {
n-th bit is set
}
else {
n-th bit is not set
}
3.Set the n-th bit :
y= x | (1<<n )
4. Unset the n-th bit :
y = x & ~(1<<n)
5. Toggle the n-th bit :
y = x ^ (1<<n)
6.Turn off the rightmost 1-bit :
y = x & (x-1)
7.Isolate the rightmost 1-bit :
y = x & (-x)
8.Right propagate the rightmost 1-bit :
y = x | (x-1)
9.Isolate the rightmost 0-bit :
y = ~x & (x+1)
10. Turn on the rightmost 0-bit :
y = x | (x+1)
11. Find maximum :
r = x ^ ((x ^ y) & -(x < y)); // max(x, y)
12. Find Minimum :
r = y ^ ((x ^ y) & -(x < y)); // min(x, y)
13.Swap between two integer :
x ^=y ^= x ^= y ;
Some Codes :
##
Print binary representation in C:
void int_to_bin(int
num) {
char str[9] =
{0}; // integer is 8 bit
int i;
for (i=7; i>=0;
i--) {
str[i] =
(num&1)?'1':'0';
num >>= 1;
}
printf("%s\n", str);
}
## Number
of 1 bits :
int
number_of_ones(unsigned
int x) {
int total_ones = 0;
while (x
!= 0) {
x = x & (x-1);
total_ones++;
}
return total_ones;
}
##
GCD CODE :
int gcd(int a, int b)
{
while(b) b ^= a ^=
b ^= a %= b;
return a;
}
Resources
:
No comments:
Post a Comment