-3

Right now I want to implement cyclic redundancy check in c++ so I wanna know how can I perform mod 2 binary division.
Well although I know that there is a string algorithm for the mod-2 binary div but I want to know if there is any int algorithm for this. Sorry for my rookie explanation.

Thank you.

Vaibhav Bisht
  • 87
  • 2
  • 14
  • 5
    `var & 1`? Is it? – 273K Oct 15 '19 at 06:40
  • what's `mod 2 binary division`? – phuclv Oct 15 '19 at 06:50
  • @phuclv mod-2 binary division is a normal division except we use xor instead of subtraction – Vaibhav Bisht Oct 15 '19 at 06:54
  • 2
    I believe OP is looking for something like this: https://math.stackexchange.com/questions/682301/modulo-2-binary-division-xor-not-subtracting-method or https://math.stackexchange.com/questions/2050028/binary-division-vs-decimal-divison It is based on some use of XOR, and is not really division (at least not in the normal sense). – Frodyne Oct 15 '19 at 06:55
  • @Frodyne yes that link was right and yeah i want a c++ implementation. And by the way it is division!(at least in normal sense) – Vaibhav Bisht Oct 15 '19 at 07:00
  • 1
    @VaibhavBisht "_i want a c++ implementation_" - You now have the algorithm. Try to implement it and return with questions (and your code) if you fail. – Ted Lyngmo Oct 15 '19 at 07:03

3 Answers3

3

Modulo 2 division uses xor operation instead of subtraction, below is the c++ implementation

#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;

char exor(char a,char b)                                      // perform exor operation
{
if(a==b)
return '0';
else
return '1';
}

void modulo2div(char data[], char key[], int datalen, int keylen)
{

char temp[20],rem[20];

for(int i=0;i<keylen;i++)
rem[i]=data[i];                    //considering keylen-1 bits of data for each step of binary division/EXOR 

for(int j=keylen;j<=datalen;j++)
{
    for(int i=0;i<keylen;i++)
    temp[i]=rem[i];                // remainder of previous step is divident of current step

    if(rem[0]=='0')                //if 1's bit of remainder is 0 then shift the rem by 1 bit
    {
        for(int i=0;i<keylen-1;i++)
            rem[i]=temp[i+1];
    }
    else                    //else exor the divident with generator polynomial
    {    
        for(int i=0;i<keylen-1;i++)
            rem[i]=exor(temp[i+1],key[i+1]);
            
    }
        rem[keylen-1]=data[j];        //appending next bit of data to remainder obtain by division
    
}
    
cout<<"CRC="<<rem<<"\nDataword="<<data;        //remainder obtain is crc

}

Visit Implementation of CRC in C++ for more details

Mrunali
  • 46
  • 2
0
#include <iostream>
using namespace std;

int main(){
      int bits[100],generator[20];

      int sizebits , sizeGen ;
      cin >> sizebits >> sizeGen ;

      for(int i = 0; i < sizebits ; i++)
           cin >> bits[i];

      for(int i = 0; i < sizeGen ; i++)
          cin >> generator[i];

      for(int i = 0; i < sizebits-sizeGen;){
            for(int j = 0; j < sizeGen; j++)
                 bits[i+j] = (bits[i+j] == generator[j]? 0 : 1) ;
            for(; i < sizebits-sizeGen && bits[i] != 1 ;i++);
      }

      for(int i = 0; i < sizebits ; i++)
          cout << bits[i];

      return 0;
}

can be done this way!

Vaibhav Bisht
  • 87
  • 2
  • 14
-2

Here is a code example for CRC, using Modulo-2 binary division):

/*
 * The width of the CRC calculation and result.
 * Modify the typedef for a 16 or 32-bit CRC standard.
 */
typedef uint8_t crc;

#define WIDTH  (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))

crc
crcSlow(uint8_t const message[], int nBytes)
{
    crc  remainder = 0; 


    /*
     * Perform modulo-2 division, a byte at a time.
     */
    for (int byte = 0; byte < nBytes; ++byte)
    {
        /*
         * Bring the next byte into the remainder.
         */
        remainder ^= (message[byte] << (WIDTH - 8));

        /*
         * Perform modulo-2 division, a bit at a time.
         */
        for (uint8_t bit = 8; bit > 0; --bit)
        {
            /*
             * Try to divide the current data bit.
             */
            if (remainder & TOPBIT)
            {
                remainder = (remainder << 1) ^ POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
    }

    /*
     * The final remainder is the CRC result.
     */
    return (remainder);

}   /* crcSlow() */

From https://barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code.

Pibben
  • 1,876
  • 14
  • 28
  • hey @Pibben can you please explain your code it will be very helpful of you, because i am not getting it thank you – Vaibhav Bisht Oct 15 '19 at 11:00
  • It's not my code. Read the tutorial in the link and see if it helps you. – Pibben Oct 15 '19 at 12:48
  • @Pibben - Please don't "lift" someone else's code, from another site, and post as your own answer, and then follow up with "it's not my code" and not explaining your answer. You could have just posted a link to the other site (as a comment, not an answer as originally posted here before you edited it), which would have been much better, as nothing would have been misrepresented here. – David Makogon Oct 16 '19 at 10:44
  • I did post only the link, but got negative feedback for that. See the first comment. I am clear in my answer that the code was taken from the link. I don't think you should down-vote my answer because I followed directions from a review. – Pibben Oct 16 '19 at 11:03