Skip to content

Commit 16e7b9b

Browse files
committed
Day-30: More Bit manipulation problems
1 parent 4a2f3a4 commit 16e7b9b

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 43 |
8-
| Current Streak | 29 |
9-
| Longest Streak | 29 ( August 17, 2015 - September 14, 2015 ) |
7+
| Total Problems | 46 |
8+
| Current Streak | 30 |
9+
| Longest Streak | 30 ( August 17, 2015 - September 15, 2015 ) |
1010

1111
</center>
1212

@@ -52,7 +52,9 @@ Include contains single header implementation of data structures and some algori
5252
| Find the parity of given number. | [find_parity.cpp](bit_manipulation/find_parity.cpp) |
5353
| Implement fast multiplication of a number to 7 using bit manipulation. | [multiply_by_7.cpp](bit_manipulation/multiply_by_7.cpp) |
5454
| Reverse bits of unsigned integer (two methods - Reversing bit by bit & divide and conquer). | [reverseBitsOfAnInteger.cpp](bit_manipulation/reverseBitsOfAnInteger.cpp) |
55-
55+
| 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)|
56+
|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)|
57+
| Given two integers, determine if their sum would be interger overflow.| [integerOverflow.cpp](bit_manipulation/integerOverflow.cpp)|
5658
### Cracking the coding interview problems
5759
| Problem | Solution |
5860
| :------------ | :----------: |

bit_manipulation/find_odd_one_out.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Given a vector of numbers, only one number occurs odd number of times, find the number
3+
* Example - { 1, 1, 2, 2, 2, 3, 3, 3, 3} ==> Answer 3
4+
* Approach - XOR of number with itself is 0, so even numbers will cancel out
5+
* and we will be left with odd number.
6+
*/
7+
8+
#include <iostream>
9+
#include <vector>
10+
11+
int find_odd_one_out( const std::vector<int> & vec )
12+
{
13+
int check = 0;
14+
for ( auto i : vec )
15+
{
16+
check ^= i;
17+
}
18+
return check;
19+
}
20+
21+
void printVector( const std::vector<int> & vec )
22+
{
23+
for ( auto & i : vec ) {
24+
std::cout << i << " ";
25+
}
26+
std::cout << std::endl;
27+
}
28+
29+
int main()
30+
{
31+
std::vector<int> vec{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
32+
std::cout << "Vector contains :" << std::endl;
33+
printVector( vec );
34+
std::cout << "Number which occurs odd time in the above vector :"
35+
<< find_odd_one_out( vec ) << std::endl;
36+
return 0;
37+
}

bit_manipulation/integerOverflow.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Given two integers, determine if their sum would be interger overflow.
3+
*/
4+
5+
#include <iostream>
6+
7+
/**
8+
* Integer overflow check
9+
* @param a [an Integer]
10+
* @param b [another Integer]
11+
* @return [returns true if a + b causes Integer overflow]
12+
*/
13+
bool integerOverflow( int a, int b)
14+
{
15+
int c = a + b;
16+
if ( a > 0 && b > 0 && c < 0 ) {
17+
return true;
18+
} else if ( a < 0 && b < 0 && c > 0 ) {
19+
return true;
20+
}
21+
else {
22+
return false;
23+
}
24+
}
25+
26+
int main()
27+
{
28+
int x = 2147483640;
29+
int y = 10;
30+
std::cout << "Sum of " << x << " and " << y << " causes interger overflow :"
31+
<< (integerOverflow( x, y ) ? "true\n":"false\n");
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Problem : One line function to return the position of right most bit.
3+
* Approach : take 2's compliment and it with number.
4+
* And finally taking a log of 2 + 1 will give us the positon
5+
*/
6+
7+
#include <iostream>
8+
#include <cmath>
9+
10+
int position_of_first_set_bit( int num )
11+
{
12+
return log2(num & -num) + 1;
13+
}
14+
15+
int main()
16+
{
17+
int num;
18+
std::cout << "Enter a number :";
19+
std::cin >> num;
20+
21+
std::cout << "Position of first set bit number in binary representation of "
22+
<< num << " is " << position_of_first_set_bit( num )
23+
<< std::endl;
24+
25+
return 0;
26+
}

0 commit comments

Comments
 (0)