Skip to content

Commit

Permalink
Day 4: Swap bits at p and q positions program
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Apr 26, 2017
1 parent acfab0c commit 39d77be
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 139 |
| Current Streak | 3 days |
| Total Problems | 140 |
| Current Streak | 4 days |
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |

</center>
Expand Down Expand Up @@ -70,7 +70,8 @@ Include contains single header implementation of data structures and some algori
| Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n right bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.|[swapSetOfBits.cpp](bit_manipulation/swapSetOfBits.cpp)|
| Add two numbers without using any arithmetic operators | [addition_without_operators.cpp](bit_manipulation/addition_without_operators.cpp)|
|Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations: <ul><li>If N is not a power of 2, reduce the counter by the largest power of 2 less than N.</li></ul><ul><li>If N is a power of 2, reduce the counter by half of N.</li></ul> The resultant value is the new N which is again used for subsequent operations.The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins. <ul><li> Given N, your task is to find the winner of the game. If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.</li></ul><ul><li>Input Format : -The first line contains an integer T, the number of testcases. T lines follow. Each line contains N, the initial number set in the counter.</ul></li> |[counter_game.cpp](bit_manipulation/counter_game.cpp)|
|Determine if two integers are of opposite signs|[check_opposite_signs.cpp](bit_manipulation/check_opposite_signs.cpp)|
|Determine if two integers are of opposite signs.|[check_opposite_signs.cpp](bit_manipulation/check_opposite_signs.cpp)|
|Swap two bits at position p and q of a given integer.| [swapBits.cpp](bit_manipulation/swapBits.cpp)|


### Cracking the coding interview problems
Expand Down
39 changes: 39 additions & 0 deletions bit_manipulation/swapBits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <bitset>

int swapBits(int n, int p, int q)
{
if (p == q)
{
return n;
}
// If bits are same, no point swapping.
// Determine if bits are different
//
if (((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q))
{
// toggle bits at p and q positions
//
n ^= (1 << p);
n ^= (1 << q);
}

return n;
}

int main()
{
int n, p, q;
std::cout << "Enter a number: ";
std::cin >> n;
std::cout << "Enter bit position 1:";
std::cin >> p;
std::cout << "Enter bit position 2:";
std::cin >> q;
int r = swapBits(n, p, q);
std::cout << "Number before swap :" << n << " (" << std::bitset<8>(n).to_string() << ") "
<< std::endl << "Number after swapping bits at position "
<< p << " and " << q << " : " << r << " (" << std::bitset<8>(r) << ")"
<< std::endl;
return 0;
}

0 comments on commit 39d77be

Please sign in to comment.