Skip to content

Commit

Permalink
Day-30: More Bit manipulation problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Sep 15, 2015
1 parent 4a2f3a4 commit 16e7b9b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 43 |
| Current Streak | 29 |
| Longest Streak | 29 ( August 17, 2015 - September 14, 2015 ) |
| Total Problems | 46 |
| Current Streak | 30 |
| Longest Streak | 30 ( August 17, 2015 - September 15, 2015 ) |

</center>

Expand Down Expand Up @@ -52,7 +52,9 @@ Include contains single header implementation of data structures and some algori
| Find the parity of given number. | [find_parity.cpp](bit_manipulation/find_parity.cpp) |
| Implement fast multiplication of a number to 7 using bit manipulation. | [multiply_by_7.cpp](bit_manipulation/multiply_by_7.cpp) |
| Reverse bits of unsigned integer (two methods - Reversing bit by bit & divide and conquer). | [reverseBitsOfAnInteger.cpp](bit_manipulation/reverseBitsOfAnInteger.cpp) |

| Small function to determine position of right most set bit in a given integer.| [right_most_set_bit.cpp](bit_manipulation/right_most_set_bit.cpp)|
|Given a vector of numbers, only one number occurs odd number of times, find the number.| [find_odd_one_out.cpp](bit_manipulation/find_odd_one_out.cpp)|
| Given two integers, determine if their sum would be interger overflow.| [integerOverflow.cpp](bit_manipulation/integerOverflow.cpp)|
### Cracking the coding interview problems
| Problem | Solution |
| :------------ | :----------: |
Expand Down
37 changes: 37 additions & 0 deletions bit_manipulation/find_odd_one_out.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Given a vector of numbers, only one number occurs odd number of times, find the number
* Example - { 1, 1, 2, 2, 2, 3, 3, 3, 3} ==> Answer 3
* Approach - XOR of number with itself is 0, so even numbers will cancel out
* and we will be left with odd number.
*/

#include <iostream>
#include <vector>

int find_odd_one_out( const std::vector<int> & vec )
{
int check = 0;
for ( auto i : vec )
{
check ^= i;
}
return check;
}

void printVector( const std::vector<int> & vec )
{
for ( auto & i : vec ) {
std::cout << i << " ";
}
std::cout << std::endl;
}

int main()
{
std::vector<int> vec{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
std::cout << "Vector contains :" << std::endl;
printVector( vec );
std::cout << "Number which occurs odd time in the above vector :"
<< find_odd_one_out( vec ) << std::endl;
return 0;
}
32 changes: 32 additions & 0 deletions bit_manipulation/integerOverflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Given two integers, determine if their sum would be interger overflow.
*/

#include <iostream>

/**
* Integer overflow check
* @param a [an Integer]
* @param b [another Integer]
* @return [returns true if a + b causes Integer overflow]
*/
bool integerOverflow( int a, int b)
{
int c = a + b;
if ( a > 0 && b > 0 && c < 0 ) {
return true;
} else if ( a < 0 && b < 0 && c > 0 ) {
return true;
}
else {
return false;
}
}

int main()
{
int x = 2147483640;
int y = 10;
std::cout << "Sum of " << x << " and " << y << " causes interger overflow :"
<< (integerOverflow( x, y ) ? "true\n":"false\n");
}
26 changes: 26 additions & 0 deletions bit_manipulation/right_most_set_bit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Problem : One line function to return the position of right most bit.
* Approach : take 2's compliment and it with number.
* And finally taking a log of 2 + 1 will give us the positon
*/

#include <iostream>
#include <cmath>

int position_of_first_set_bit( int num )
{
return log2(num & -num) + 1;
}

int main()
{
int num;
std::cout << "Enter a number :";
std::cin >> num;

std::cout << "Position of first set bit number in binary representation of "
<< num << " is " << position_of_first_set_bit( num )
<< std::endl;

return 0;
}

0 comments on commit 16e7b9b

Please sign in to comment.