Advertisement
urmisaha

Gray to binary code

Nov 20th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. /** Gray code to binary code
  2. Minimum number of operations required to convert a binary number to 0 using the method:
  3. change ith binary digit only if (i+1)th binary digit is 1 and all digits following that is 0.
  4. **/
  5.  
  6. #include <bits/stdc++.h>
  7. #define ll long long
  8. using namespace std;
  9.  
  10.  
  11. int main() {
  12.     string S;
  13.     cin>>S;
  14.     ll fact = 1;
  15.     int prev = S[0]-'0';
  16.     ll ans = prev;
  17.     for(ll i=1; i<S.size(); ++i){
  18.         prev = prev^(S[i]-'0');
  19.         ans = ans*2 + prev;
  20.     }
  21.     cout<<ans;
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement