-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion26.c
69 lines (54 loc) · 826 Bytes
/
question26.c
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
62
63
64
65
66
67
68
69
/*
http://practice.geeksforgeeks.org/problems/swap-all-odd-and-even-bits/0
Swap all odd and even bits
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int modify(int n){
int arr[8];
int temp = n;
int rem = temp%2;
int quo;
if(rem == 0){
quo = temp/2;
}else{
quo = (temp-1)/2;
}
int i = 0;
arr[i] = rem;
i++;
while(quo > 0){
rem = quo%2;
arr[i] = rem;
quo = (quo%2 == 0)? quo/2: (quo-1)/2;
i++;
}
arr[i] = quo;
int j = i;
for(j=i+1;i<8;i++){
arr[i] = 0;
}
for(k=0;k<8;k++){
int b = arr[k];
arr[k] = arr[k+1];
arr[k+1] = b;
k++;
}
int num = 0;
for(j=0;j<8;j++){
num += arr[j]*pow(2,j);
}
return num;
}
int main(){
int cases;
scanf("%d",&cases);
while(cases > 0){
int n;
scanf("%d",&n);
printf("%d\n", modify(n));
cases--;
}
return 0;
}