#include using namespace std; string Ones_complement(string data){ for (int i = 0; i < data.length(); i++) { if (data[i] == '0') data[i] = '1'; else data[i] = '0'; } return data; } string Twos_complement(string data) { bool carry = true; for (int i = 0; i < data.length(); i++) { if (data[i] == '0') data[i] = '1'; else data[i] = '0'; } for (int i = data.length() - 1; i >= 0; i--) { if (data[i] == '1' && carry) { data[i] = '0'; } else if (carry) { data[i] = '1'; carry = false; } } return data; } string check(string s , int block , int complement){ int n = s.length(); if(n%block !=0){ int add = block - (n%block); for(int i=0 ; i=0 ; k--){ sum += (next[k] - '0') + (ans[k] - '0') ; carry = sum/2; if(sum==0){ addition = '0' + addition; sum = carry;} else if(sum==1) {addition = '1' + addition; sum=carry;} else if(sum==2){ addition = '0' + addition; sum = carry; } else{ addition = '1' + addition; sum=carry; } } string final ; if (carry == 1) { for (int l = addition.length() - 1; l >= 0;l--) { if (carry == 0) { final = addition[l] + final; } else if (((addition[l] - '0') + carry) % 2 == 0) { final = "0" + final; carry = 1; } else { final = "1" + final; carry = 0; } } ans = final; } else { ans = addition; } } if(complement==1) return Ones_complement(ans); else return Twos_complement(ans); } bool checkSum(string in ,string rec , int block , int complement){ string in_c = check(in , block , complement); string rec_c = check((rec+in_c) , block , complement); if(complement ==1) { if(count(rec_c.begin(),rec_c.end(),'0') == block){ return true; } return false; } else if(complement == 2){ if(count(rec_c.begin(),rec_c.end(),'1') == block){ return true; } return false; } return false; } int main() { string in,rec; cout << "Send Data" << endl; cin>>in; cout << "Recieve Data" << endl; cin>>rec; int block ; cout << "Enter Block Size" << endl; cin>>block; int complement; cout << "Enter 1 for 1's complement or 2 for 2's complement"<< endl; cin>>complement; bool flag = checkSum(in , rec , block , complement); if(flag) cout<<"No error\n"; else cout<<"error\n"; return 0; }