-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL008.cpp
56 lines (49 loc) · 1.02 KB
/
SCTDL008.cpp
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
/**
* @file SCTDL008.cpp
* @author long ([email protected])
* @brief Convert binary string to gray code string format.
* @version 0.1
* @date 2023-02-27
*
* @copyright Copyright (c) 2023
*
*/
#include <iostream>
using namespace std;
// function to xor two characters
char xor_c(char a, char b)
{
return (a == b) ? '0' : '1';
}
// function to flip the bit
char flip(char c)
{
return (c == '0') ? '1' : '0';
}
// function to convert binary string
// to gray string
string binarytoGray(string binary)
{
string gray = "";
// MSB of gray code is same as binary code
gray += binary[0];
// Compute remaining bits, next bit is computed by
// doing XOR of previous and current in Binary
for (int i = 1; i < binary.length(); i++) {
// Concatenate XOR of previous bit
// with current bit
gray += xor_c(binary[i - 1], binary[i]);
}
return gray;
}
int main()
{
int t;
cin >> t;
while(t--) {
string binary;
cin >> binary;
cout << binarytoGray(binary) << endl;
}
return 0;
}