Skip to content

Instantly share code, notes, and snippets.

@saisumit
Last active November 1, 2016 20:08
Show Gist options
  • Select an option

  • Save saisumit/1200b23e6f8b4e2a23172112d1022d56 to your computer and use it in GitHub Desktop.

Select an option

Save saisumit/1200b23e6f8b4e2a23172112d1022d56 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < n; i++)
unsigned int message = 0 ;
unsigned int POLYNOMIAL = 0 ;
unsigned int rem = 0 ;
unsigned int CRC( )
{
rem = message;
for ( int bit = 31; bit > 0; --bit)
{
if (rem & (1<<31) ) // checking the left most bit
rem ^= POLYNOMIAL; // xor the remainder with the polynomial
rem = (rem << 1); // left shift at each iteration
}
return (rem >> 4); // only last four bits to be returned ;
}
int main ( )
{
string s ;
cout<<"Enter the received string" << endl ;
cin >> s ;
int n = s.length( ) ;
vector<int> A( n ) ;
for( int i = n - 1 ; i >= 0 ; i -- )
if( s[i] == '1' )
message+=(1<<(n-1-i)) ;
cin >> s ;
n = s.length() ;
for( int i = n - 1 ; i >= 0 ; i -- )
if( s[i] == '1' )
POLYNOMIAL+=(1<<(n-1-i)) ;
cout<< CRC( ) << endl ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment