|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +int isOdd(unsigned int x){ // Returns 1 if Odd, 0 if even // printf("%s", isOdd(a) ? "Odd" : "Even"); |
| 4 | + |
| 5 | + if(x & 1u){ |
| 6 | + return 1; |
| 7 | + } |
| 8 | + return 0; |
| 9 | +} |
| 10 | + |
| 11 | +/*****************************************************************************************************/ |
| 12 | + |
| 13 | +int areOppSigns(int x, int y){ // Returns 1 if Opp, 0 if Same |
| 14 | + |
| 15 | + |
| 16 | + if ((x ^ y) < 0){ // Opp sign |
| 17 | + return 1; |
| 18 | + } |
| 19 | + return 0; |
| 20 | +} |
| 21 | + |
| 22 | +/*****************************************************************************************************/ |
| 23 | + |
| 24 | +/********** Add 1 to an integer ***************/ |
| 25 | + |
| 26 | +// Use the fact that 2's complement of a number (how -number is stored) is (1+inv(number)) |
| 27 | +// -x = ~x + 1; |
| 28 | +// -~x = 1+x |
| 29 | + |
| 30 | +int addOne(int x){ // Returns result |
| 31 | + |
| 32 | + int ans; |
| 33 | + ans = -(~x); |
| 34 | + return ans; |
| 35 | +} |
| 36 | + |
| 37 | +/*****************************************************************************************************/ |
| 38 | + |
| 39 | +/******** Swap two numbers w/o third variable **********/ |
| 40 | + |
| 41 | +// x ^ x = 0 |
| 42 | +// x ^ 0 = x |
| 43 | + |
| 44 | +void Swap(int * x, int * y){ |
| 45 | + |
| 46 | + *x = *x ^ *y; |
| 47 | + *y = *x ^ *y; |
| 48 | + *x = *x ^ *y; |
| 49 | + |
| 50 | + return; |
| 51 | +} |
| 52 | + |
| 53 | +/*****************************************************************************************************/ |
| 54 | + |
| 55 | +/******* Turn off kth bit in a number ***************/ |
| 56 | + |
| 57 | +// make a MACRO that takes in the position k, and makes a MASK for that position |
| 58 | + |
| 59 | +#define MASK(k) (1u << (k)) |
| 60 | + |
| 61 | +int kthOff (int x, int k){ |
| 62 | + |
| 63 | + int ans; |
| 64 | + ans = x & (~(MASK(k))); |
| 65 | + |
| 66 | + return ans; |
| 67 | +} |
| 68 | + |
| 69 | +/*****************************************************************************************************/ |
| 70 | + |
| 71 | +/******* Turn on kth bit in a number ***************/ |
| 72 | + |
| 73 | +//#ifndef MASK(k) // ifndef doesn't work on MACROFUNCTION ? |
| 74 | +//#define MASK(k) (1u << k) |
| 75 | + |
| 76 | +//#endif |
| 77 | + |
| 78 | +// using the same MACROFUNCTION created above |
| 79 | + |
| 80 | +void kthOn (int* x, int k){ |
| 81 | + |
| 82 | + *x |= MASK(k); |
| 83 | + |
| 84 | + return; |
| 85 | +} |
| 86 | + |
| 87 | +/*****************************************************************************************************/ |
| 88 | + |
| 89 | +/******* Check if kth bit is set in a number ***************/ |
| 90 | + |
| 91 | +// using the same MACROFUNCTION created above |
| 92 | + |
| 93 | +int iskthSet(int x, int k){ |
| 94 | + |
| 95 | + return (x & MASK(k)); |
| 96 | +} |
| 97 | + |
| 98 | +/*****************************************************************************************************/ |
| 99 | + |
| 100 | +/******* Toggle the kth bit ***************/ |
| 101 | + |
| 102 | +// using the same MACROFUNCTION created above |
| 103 | + |
| 104 | +void togglekth(int* x, int k){ |
| 105 | + |
| 106 | + *x ^= MASK(k); |
| 107 | +} |
| 108 | + |
| 109 | +/*****************************************************************************************************/ |
| 110 | + |
| 111 | +/******* Unset the righmost-set bit ***************/ |
| 112 | + |
| 113 | +// performing n & (n-1) unsets the rightmost-set-bit in n |
| 114 | +// (n-1) has all the bits AFTER the rightmost-set-bit flipped w.r.t n |
| 115 | + |
| 116 | +int unsetrightmostset (int x){ // Returns result |
| 117 | + |
| 118 | + return (x & (x-1)); |
| 119 | +} |
| 120 | + |
| 121 | +/*****************************************************************************************************/ |
| 122 | + |
| 123 | +/******* Check if number is a power of 2 ***************/ |
| 124 | + |
| 125 | +// put the righmost-set-bit concept to use |
| 126 | +// pwr of 2 will only have one bit set in total |
| 127 | +// so, if pwrof2, then unsetting that bit should yield 0 |
| 128 | + |
| 129 | +int ispwroftwo (int x){ // Returns 1 if true, 0 false |
| 130 | + |
| 131 | + //(x & (x-1)) ? return (0) : return (1); //error: expected expression before return |
| 132 | + |
| 133 | + if(x&(x-1)) |
| 134 | + return 0; |
| 135 | + else |
| 136 | + return 1; |
| 137 | +} |
| 138 | + |
| 139 | +/*****************************************************************************************************/ |
| 140 | + |
| 141 | +/******* Find position of the righmost-set-bit ***************/ |
| 142 | + |
| 143 | +// AGAIN put the righmost-set-bit concept to use |
| 144 | +// unset the rmost-set-bit ; then XOR the result with the origl number --> only that bit will be set |
| 145 | + |
| 146 | +int rmostsetpos (int x){ |
| 147 | + |
| 148 | + int y = x; |
| 149 | + int pos = -1; // BCZ I want rmost-bit as pos = 0 |
| 150 | + x &= x-1; |
| 151 | + y ^= x; |
| 152 | + |
| 153 | + while(y){ |
| 154 | + y = y >> 1; |
| 155 | + pos++; |
| 156 | + } |
| 157 | + return pos; |
| 158 | + |
| 159 | +} |
| 160 | + |
| 161 | +/*****************************************************************************************************/ |
| 162 | + |
| 163 | + |
| 164 | +/******* Uppercase to lowercase, vice-versa, and Toggle***************/ |
| 165 | + |
| 166 | +// A is 0100 0001, a is 0110 0001 |
| 167 | +// Space character (' ') is 0x20 |
| 168 | +// Hence, simply Bitwise-OR A with ' ' to get a |
| 169 | +// And, a to A by Bitwise-AND a with Underscore character('_') |
| 170 | + |
| 171 | +// And to toggle b/n Uppcase and Lowcase, simply XOR with ' ' |
| 172 | + |
| 173 | + |
| 174 | +/*****************************************************************************************************/ |
| 175 | + |
| 176 | +/******* Count the number of set-bits ***************/ |
| 177 | + |
| 178 | +// unset rmost-set-bit until 0 set bits left ? |
| 179 | + |
| 180 | +int countsetbits (int x){ |
| 181 | + |
| 182 | + int count = 0; |
| 183 | + while (x){ |
| 184 | + x &= x-1; // this unsets the rmost-set-bit |
| 185 | + count++; |
| 186 | + } |
| 187 | + return count; |
| 188 | +} |
| 189 | + |
| 190 | +/*****************************************************************************************************/ |
| 191 | + |
| 192 | +/******* Swap endianness using bmpln ***************/ |
| 193 | + |
| 194 | +unsigned int swEndn (unsigned int x){ // 0x 1234abcd ---> 0x cdab3412 |
| 195 | + |
| 196 | + x = (x >> 16) | (x << 16); // 0x abcd1234 |
| 197 | + x = ((x & (0xff00ff00)) >> 8) | ((x & (0x00ff00ff)) << 8); |
| 198 | + |
| 199 | + return x; |
| 200 | +} |
| 201 | + |
| 202 | +/*****************************************************************************************************/ |
| 203 | + |
| 204 | +/******* Generate an array with number of set-bits in each index value ***************/ |
| 205 | + |
| 206 | +// for any value n, generate an array that is n elements long |
| 207 | + |
| 208 | +void genarr (int n){ |
| 209 | + |
| 210 | + unsigned int arr[n]; |
| 211 | + |
| 212 | + for (int i=0; i<n; i++){ |
| 213 | + |
| 214 | + arr[i] = countsetbits(i); |
| 215 | + } |
| 216 | + for (int i=0; i<n; i++){ |
| 217 | + |
| 218 | + printf("%d, ", arr[i]); |
| 219 | + } |
| 220 | + |
| 221 | + return; |
| 222 | +} |
| 223 | + |
| 224 | +int main(){ |
| 225 | + |
| 226 | + int n = 5; |
| 227 | + |
| 228 | + genarr(n); |
| 229 | + |
| 230 | + return 0; |
| 231 | +} |
| 232 | + |
0 commit comments