-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path127_rotate_bits.cpp
More file actions
61 lines (56 loc) · 895 Bytes
/
Copy path127_rotate_bits.cpp
File metadata and controls
61 lines (56 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// https://practice.geeksforgeeks.org/problems/rotate-bits/0
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
{
int n, d;
cin >> n >> d;
int m1 = pow(2, 15), m2 = 1;
d = d % 16;
// left rotation
int times = d, num = n;
while (times--)
{
// if 16 bit is set add it on right side
if (num & m1)
{
// left shift
num *= 2;
num = num | 1;
}
else
{
num *= 2;
}
}
cout << num << endl;
// right rotation
times = d, num = n;
while (times--)
{
// if 16 bit is set add it from left side
if (num & 1)
{
// right shift
num /= 2;
num = num | m1;
}
else
{
num /= 2;
}
}
cout << num << endl;
}
return 0;
}