Skip to content

Commit 39d77be

Browse files
committed
Day 4: Swap bits at p and q positions program
1 parent acfab0c commit 39d77be

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 139 |
10-
| Current Streak | 3 days |
9+
| Total Problems | 140 |
10+
| Current Streak | 4 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -70,7 +70,8 @@ Include contains single header implementation of data structures and some algori
7070
| 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)|
7171
| Add two numbers without using any arithmetic operators | [addition_without_operators.cpp](bit_manipulation/addition_without_operators.cpp)|
7272
|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)|
73-
|Determine if two integers are of opposite signs|[check_opposite_signs.cpp](bit_manipulation/check_opposite_signs.cpp)|
73+
|Determine if two integers are of opposite signs.|[check_opposite_signs.cpp](bit_manipulation/check_opposite_signs.cpp)|
74+
|Swap two bits at position p and q of a given integer.| [swapBits.cpp](bit_manipulation/swapBits.cpp)|
7475

7576

7677
### Cracking the coding interview problems

bit_manipulation/swapBits.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <bitset>
3+
4+
int swapBits(int n, int p, int q)
5+
{
6+
if (p == q)
7+
{
8+
return n;
9+
}
10+
// If bits are same, no point swapping.
11+
// Determine if bits are different
12+
//
13+
if (((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q))
14+
{
15+
// toggle bits at p and q positions
16+
//
17+
n ^= (1 << p);
18+
n ^= (1 << q);
19+
}
20+
21+
return n;
22+
}
23+
24+
int main()
25+
{
26+
int n, p, q;
27+
std::cout << "Enter a number: ";
28+
std::cin >> n;
29+
std::cout << "Enter bit position 1:";
30+
std::cin >> p;
31+
std::cout << "Enter bit position 2:";
32+
std::cin >> q;
33+
int r = swapBits(n, p, q);
34+
std::cout << "Number before swap :" << n << " (" << std::bitset<8>(n).to_string() << ") "
35+
<< std::endl << "Number after swapping bits at position "
36+
<< p << " and " << q << " : " << r << " (" << std::bitset<8>(r) << ")"
37+
<< std::endl;
38+
return 0;
39+
}

0 commit comments

Comments
 (0)