diff --git a/README.md b/README.md index f8ff2c6..301916e 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ This repository will contain random algorithm and data structure problems I resolve to solve at least one a day. ## Current Streak -**23 days** +**25 days** ## Longest streak -23 days (August 17, 2015 - Sept 08, 2015) +25 days (August 17, 2015 - Sept 10, 2015) ## Include Directiory Include directory and sub directories contain STL like header file implementation of various algorithms and data structures. Following header only implementation, @@ -52,3 +52,6 @@ please let me know. ### Bit Manipulation - Determine if a number is a power of 2. - Add two binary number represented as string. + - Determine the next power of 2 for a given number. + - Using bit manipulation determine if a number is multiple of 3. + - Determine endianess of the machine, print a number in reverse Endianess. diff --git a/bit_manipulation/multiple_of_3.cpp b/bit_manipulation/multiple_of_3.cpp new file mode 100644 index 0000000..65e34da --- /dev/null +++ b/bit_manipulation/multiple_of_3.cpp @@ -0,0 +1,68 @@ +/** + * Check if a given number is multiple of 3 + * Basic way of doing is : if sum of digits of number is multiple of 3, + * number is divisible by 3. + * + * However another efficient way of doing it is: + * Count the number of set bits at even positions. + * Count the number of set bits at odd positions. + * if difference is multiple of 3, number will be multiple of 3. + * + * Proof: + * We can prove it by taking the example of 11 in decimal numbers. + * (It will apply to 3 in binary numbers as well.) + * AB = 10A + B ( A and B are digits of number AB) + * AB = 11A + (B - A) + * Clearly if (B-A) is multiple of 11, number would be muliple of 11. + * We can do it similarly for 3 digits + * ABC = 100A + 10B + C + * = 99A + A + 11B - B + C + * = 11(9A + B) + (A - B + C) + * Clearly if A-B+C is multiple of 11, ABC would be multiple as well. + * similarly for 4 digits + * ABCD = 1000A + 100B + 10C + D + * = (1001A – 999B + 11C) + (D + B – A -C ) + * So, if (B + D – A – C) is a multiple of 11 then is ABCD. + */ + +#include + +bool is_multiple_of_3( int n ) +{ + // change to positive if negative + if ( n < 0 ) { + n = -n; + } + if ( n == 0 ) { + return true; + } + if ( n == 1 ) { + return false; + } + int even_count = 0; + int odd_count = 0; + while ( n ) { + if ( n & 1 ) { + ++odd_count; + } + n >>= 1; + if ( n & 1 ) { + ++even_count; + } + n >>= 1; + } + return is_multiple_of_3( even_count - odd_count ); +} + +int main() +{ + int num; + std::cout << "Enter a number:"; + std::cin >> num; + if (is_multiple_of_3(num)) { + std::cout << num << " is multiple of 3" << std::endl; + } else { + std::cout << num << " is not a multiple of 3" << std::endl; + } + return 0; +} diff --git a/bit_manipulation/next_power_of_2.cpp b/bit_manipulation/next_power_of_2.cpp new file mode 100644 index 0000000..10fc903 --- /dev/null +++ b/bit_manipulation/next_power_of_2.cpp @@ -0,0 +1,28 @@ +/* + * Write a function that, for a given no n, finds a number p which is greater than or equal to n and is a power of 2. + */ +#include + +int next_power_of_2( int num ) { + //already a power of 2 + if (num && !(num & (num-1))) { + return num; + } + //count till msb set bit + int count = 0; + while ( num != 0 ) { + num >>= 1; + count++; + } + return (1 << count); +} + +int main() +{ + std::cout << "Enter a number:"; + int num; + std::cin >> num; + std::cout << "Next power of 2 which is greater than or equal to " << num + << " is: " << next_power_of_2(num) << std::endl; + return 0; +} diff --git a/bit_manipulation/reverseEndianness.cpp b/bit_manipulation/reverseEndianness.cpp new file mode 100644 index 0000000..b09db89 --- /dev/null +++ b/bit_manipulation/reverseEndianness.cpp @@ -0,0 +1,68 @@ +#include +#include + +enum endianess { + LITTLE_ENDIAN_MACHINE = 0, + BIG_ENDIAN_MACHINE +}; + +endianess determine_endianess() +{ + unsigned int num = 1; + char * c = (char *) # + if (*c == 1) { + return LITTLE_ENDIAN_MACHINE; + } else { + return BIG_ENDIAN_MACHINE; + } +} + +void printBytes( char * start, int size) +{ + for ( int i = 0; i < size; ++i ) { + printf("%.2x ", start[i] ); + } + std::cout << std::endl; +} + +int reverseEndianNess( int num ) +{ + int byte1, byte2, byte3, byte4; + byte1 = (num & 0x000000FF) >> 0; + byte2 = (num & 0x0000FF00) >> 8; + byte3 = (num & 0x00FF0000) >> 16; + byte4 = (num & 0xFF000000) >> 24; + return ((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | (byte4 << 0)); +} + +int main() { + endianess sys_endianess = determine_endianess(); + if (sys_endianess == LITTLE_ENDIAN_MACHINE) { + std::cout << "System is little endian\n\n"; + } else { + std::cout << "System is big endian\n\n"; + } + + int num = 0x01234567; + std::cout << "Num in decimal: " << num << std::endl; + std::ios::fmtflags f(std::cout.flags()); + std::cout << "Num in hexadecimal:" << std::hex << num << std::endl; + std::cout.flags( f ); + std::cout << "Printing individual bytes:\n"; + printBytes((char*)&num, sizeof(num)); + + std::cout << std::endl; + std::cout << "Num in reversed endianness:\n"; + int num1 = reverseEndianNess(num); + + std::cout << "Num in decimal :" << num1 << std::endl; + + f = std::cout.flags(); + std::cout << "Num in hexadecimal:" << std::hex << num1 << std::endl; + std::cout.flags( f ); + std::cout << "Printing individual bytes:\n"; + printBytes((char*)&num1, sizeof(num1)); + + return 0; + +}