|
| 1 | +## Bitwise |
| 2 | + |
| 3 | +* **Bitwise algorithms:** used to perform operatiosn at bit-level. |
| 4 | + |
| 5 | +## Bitwise Operatos |
| 6 | + |
| 7 | +* &(bitwise AND): takes two numbers as operands: Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. Suppose A = 5 and B = 3, therefore A & B = 1. |
| 8 | +* |(bitwise OR): Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1. Suppose A = 5 and B = 3, therefore A | B = 7. |
| 9 | +* ^ (bitwise XOR): takes to numbers and return the XOR results of the them |
| 10 | +* << (left shift): left shift the bits of the first operand, the second indicates the number of places to shif. |
| 11 | +* >> (right shift): right shift the bits of the first operand, the second indicates the number of places to shif. |
| 12 | +* ~ (bitwise NOT): it inverts all the bits. (Es: A = 4; ~A = 3) |
| 13 | + |
| 14 | +## 'XOR' operator. |
| 15 | +1^1 = 0 |
| 16 | +0^0 = 0 |
| 17 | +1^0 = 1 |
| 18 | +0^1 = 1 |
| 19 | + |
| 20 | +## Even and Odd |
| 21 | + |
| 22 | +* Even: has first bit (from right hand side):set to 0; |
| 23 | +* Odd: has first bit (from right hand side): set to 1; |
| 24 | + |
| 25 | +## Arithmetic operations: |
| 26 | + |
| 27 | +* Divide by 2: |
| 28 | +``` |
| 29 | +x = 10; |
| 30 | +x = x >> 1; // (x is now 5)80 |
| 31 | +``` |
| 32 | +* Multiply by 2: |
| 33 | +``` |
| 34 | +x = 20; |
| 35 | +print(x << 1); // x is now 40 |
| 36 | +print(x << 2); // x is now 80 |
| 37 | +print(x << 3); // x is now 160 |
| 38 | +
|
| 39 | +/* |
| 40 | +// Ex by: Geeks for geeks |
| 41 | +x = 18(00010010) |
| 42 | +x << 1 = 36 (00100100) |
| 43 | +*/ |
| 44 | +``` |
| 45 | + |
| 46 | +* Find log base of a integer |
| 47 | +``` |
| 48 | +int log2_calculator(int num) |
| 49 | +{ |
| 50 | + int counter = 0; |
| 51 | + while( x>>=1 ) |
| 52 | + counter++; |
| 53 | + // The number of times that we have applied the shift operator is the log2 of our number |
| 54 | + return res; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Bit operations and problems |
| 59 | + |
| 60 | +* Set n'th bit of a number: |
| 61 | +We need to shift 1 k times to its left and the perform bitwise OR operation with the number and result of left shift performed just before. |
| 62 | +``` |
| 63 | +// function to set the kth bit |
| 64 | +int setKthBit() |
| 65 | +{ |
| 66 | + int n = 4; // Integer were we need to set the nth bit |
| 67 | + int k =1; // Starting from the 0-based index at the right hand side, we will set the |
| 68 | + // bit at the first position (Position k) |
| 69 | + // kth bit of n is being set by this operation |
| 70 | + |
| 71 | + // The result will not be 6 |
| 72 | + // 4 in binary: 1 0 0 |
| 73 | + // After have set the kth bit: 1 1 0 |
| 74 | + return ((1 << k) | n); |
| 75 | +} |
| 76 | +``` |
| 77 | +* Unseat n'th position of a number: |
| 78 | +``` |
| 79 | + // Do & of n with a number with all set bits except |
| 80 | + // the k'th bit |
| 81 | + return (n & ~(1 << (k - 1))); |
| 82 | +``` |
| 83 | +* Check if a bit at nth position is set (1) or unset(0) |
| 84 | + |
| 85 | +1. Left shift given number 1 by k-1 to create |
| 86 | + a number that has only set bit as k-th bit. |
| 87 | + temp = 1 << (k-1) |
| 88 | +2. If bitwise AND of n and temp is non-zero, |
| 89 | + then result is SET else result is NOT SET. |
| 90 | +``` |
| 91 | +bool isSet(int number, int position) |
| 92 | +{ |
| 93 | + if (number & (1 << (position - 1))) |
| 94 | + return true; |
| 95 | + else |
| 96 | + return false; |
| 97 | +} |
| 98 | +``` |
| 99 | +* Flip bits of a given number |
| 100 | +It can be done by a simple way, just simply subtract the number from the value obtained when all the bits are equal to 1 . |
| 101 | + |
| 102 | +* Find most significant set bit in the given number |
| 103 | + |
| 104 | +The most-significant bit in binary representation of a number is the highest ordered bit, that is it is the bit-position with highest value. |
| 105 | + |
| 106 | + |
| 107 | +* Given a number N, the task is to check whether the given number is a power of 2 or not |
| 108 | +``` |
| 109 | +// Function to check if x is power of 2 |
| 110 | +bool isPowerOfTwo(int number) |
| 111 | +{ |
| 112 | + if(number==0) |
| 113 | + return false; |
| 114 | + |
| 115 | + return (ceil(log2(number)) == floor(log2(number))); |
| 116 | +} |
| 117 | +``` |
| 118 | +OR |
| 119 | +``` |
| 120 | +/* Function to check if the given number is power of 2*/ |
| 121 | +bool isPowerOfTwo (int number) |
| 122 | +{ |
| 123 | + |
| 124 | + return number && (!(number&(number-1))); |
| 125 | +} |
| 126 | +``` |
0 commit comments