C++ Program which line of coding do you use to find the position of the leftmost
ID: 3715398 • Letter: C
Question
C++ Program which line of coding do you use to find the position of the leftmost '1' for lining up the polynomial? Why do you choose that approach?
Code is below.
#include
#include
using namespace std;
char to_upper(char c)
{
if (c >= 'a' && c <= 'z')
return c - 32;
return c;
}
string performCRC(string &msg, string generator)
{
int i = 0, j, len = msg.length(), g_len = generator.length();
string modified;
while (i < len)
{
modified += msg[i];
i++;
}
cout<<"The initial data is: "<
for (i = 0; i < len - g_len + 1; i++)
{
if (modified[i] == '1')
{
for (j = 0; j < g_len; j++)
{
if (modified[j + i] == generator[j])
{
modified[i + j] = '0';
}
else
{
modified[i + j] = '1';
}
}
cout<<"The intermediate result is: "<
}
modified[i + g_len] = msg[i + g_len];
}
cout<<"The final result is: "<
j = 0;
string rem ="";
int r_ind = len-g_len+1;
while (j
{
rem += modified[j+r_ind];
msg[j+r_ind] = modified[j+r_ind];
j++;
}
return rem;
}
string convertBinary(string input)
{
int i = 0, len = input.length();
string msg;
while(i
msg += "0000";
i++;
}
i=0;
while (i
{
int val;
if (input[i] >= '0' && input[i] <= '9')
val = (int)input[i] - '0';
else
val = (int)to_upper(input[i]) - 55;
int k = 3;
while (val != 0)
{
msg[i * 4 + k] = (val % 2) == 0 ? '0' : '1';
val = val / 2;
k--;
}
i++;
}
return msg;
}
int main()
{
string msg, input, generator = "10011";
cout<<"Enter input message in Hex: ";
cin>>input;
msg = convertBinary(input);
cout<<"Input message in Binary: "<< msg<
cout<<"The polynomial in binary: "<< generator<
int i = 0, len = msg.length(), g_len = generator.length();
while (i < g_len-1)
{
msg += '0';
i++;
}
cout<<"Modified message is: "<< msg<
string rem = performCRC(msg, generator);
cout<<"The CRC is : "<< rem<
cout<<"Final append message is: "<< msg<
cout<
cout<<"Enter input message in hex: ";
cin>>input;
msg = convertBinary(input);
cout<<"Input message in Binary: "<< msg<
len = msg.length();
for (i = 0; i < g_len-1; i++)
{
msg += rem[i];
}
rem = performCRC(msg, generator);
int c = 0;
i = 0;
while (rem[i] != '')
{
if (rem[i] == '0')
c++;
i++;
}
if (c == g_len-1)
cout<<"Verification successfull!"<
else
{
cout<<"Verification failed!"<
}
cout<
return 0;
}
Explanation / Answer
The statement
if (modified[i] == '1')
inside the loop of function "string performCRC(string &msg, string generator)" is to find the position of the leftmost '1' for lining up the polynomial.
The function is performing an operation in the form of the string rather then actual bit sequence. It is adopted so because handling number of 0's or 1's in given number and finding leftmost or right most set bit repeatedly is much easier than finding it by another mathematical or bitwise operations.
this computationally fast method and saves the effort of many division and multiplication.
since in CRC we find first set bit and then we bitwise xor the data with CRC generator.
Hence after finding the leftmost set bit in first loop i.e. starting from i=0, next loop is updating the string 'Modified' by xor of above mentioned two and again it will search for next 1,
Hit thumbs up if you like the answer.
Feel free to ask any doubt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.