Skip to content

Commit

Permalink
Day-25: Bit manipulation problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Sep 10, 2015
1 parent 159cd6e commit 7a4e193
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
68 changes: 68 additions & 0 deletions bit_manipulation/multiple_of_3.cpp
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;
}
28 changes: 28 additions & 0 deletions bit_manipulation/next_power_of_2.cpp
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;
}
68 changes: 68 additions & 0 deletions bit_manipulation/reverseEndianness.cpp
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 *) &num;
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;

}

0 comments on commit 7a4e193

Please sign in to comment.