-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <iostream> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <iostream> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <iostream> | ||
#include <cstdio> | ||
|
||
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; | ||
|
||
} |