title: Binary division issue: bad examples on internet or what am I missing?
body: <p>I would like to create a 16 bit CRC. Actually I am entirely ready whit that, so a couple of hours ago I tested it, but did not work properly. But what I discovered is that the examples on the internet might be wrong when its about binary division. I link just two of them (from a lot!): <a href="http://www.ross.net/crc/download/crc_v3.txt" rel="nofollow">http://www.ross.net/crc/download/crc_v3.txt</a> , <a href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check" rel="nofollow">http://en.wikipedia.org/wiki/Cyclic_redundancy_check</a></p>
  
  <p>So what I am doing with the binary division is that I use BigIntegers. Just like this:</p>
  
  <pre><code>BigInteger divisor = new BigInteger("10011", 2);
   BigInteger dividend = new BigInteger("11010110110000", 2);
   BigInteger[] result = dividend.divideAndRemainder(divisor);
  </code></pre>
  
  <p>These values can be seen in the first link (6. fully worked example). Lets see the results, the reference gives me 1110 for the remainder, my upper code gives me 111. Might be code wrong? I dont think so.. Lets count it in decinal: 13744/19 = 723 and remainder is 7.. So the code gives me 111 which is actually 7 in decimal, but the example gives me 1110, which is 14. It is wrong isn'it?</p>
  
  <p>If you see the wikipedia example, it gives wrong again, both for the 1st and both for the 2nd calculation. When I count them with the upper code, it gives me a good result in decimal. And almost all the examples I checked, I got the same results. So actually, what the hell am i missing? </p>
  
  <h2>--------------------------------------------------------</h2>
  
  <h2>UPDATE: WORKING CODE</h2>
  
  <p>Hey kiddos, as I promised I provide a fully-wroking binary divison which <strong>uses CRC arithmetic</strong>! If you want to use polynomial arithetmic the upper code is completely OK!! Just use the <code>getRemainder()</code> method and add two Strings, it counts the remainder. <strong>Remember</strong>: if your bits arrived correctly, the remainder will be 0! In this example, it gives you a string with 0 length! Use it with fun!</p>
  
  <pre><code>public class BinaryDivision {
  
  
      String substracted;
  
      public BinaryDivision(){
  
      }
  
      public String getRemainder(String dividend, String divisor){
  
          int dividendLength = dividend.length();
          int divisorLength = divisor.length();
  
          String quotient="";
          String examinedP="";
          String divisorP="";
          substracted="";
  
          int indexNumber;
          int substractZeros=0;
  
  
          for (int i=0;i&lt;divisorLength;i++){
              examinedP = examinedP + dividend.charAt(i);
          }
  
  
          indexNumber = divisorLength;
  
          for (int j=0;j&lt;(dividendLength-divisorLength);j++){
  
              //START
              if ( Integer.parseInt(String.valueOf(examinedP.charAt(0)))==1){
  
  
                  quotient=quotient + "1";
  
                  int a = divisor.length();
                  //substracting
                  for (int i = 0;i&lt;a;i++){
                  //  System.out.println(examinedP.charAt(i) + "  " +  divisor.charAt(i));
  
                      substracted = substracted +
                              CRC_substract(Integer.parseInt(String.valueOf(examinedP.charAt(i))),
                                      Integer.parseInt(String.valueOf(divisor.charAt(i))));
                  }
  
                  substracted = substracted.substring(1);
                  //----------------------------------------------
                  substracted = substracted + dividend.charAt(indexNumber);
  
              }
              else {
                  quotient=quotient + "0";
                  int a = divisor.length();
  
                  //substracting
                  for (int i = 0;i&lt;a;i++){
                      //System.out.println(examinedP.charAt(i) + "  " +  0);
  
                      substracted = substracted +
                              CRC_substract(Integer.parseInt(String.valueOf(examinedP.charAt(i))),
                                      0);
                  }
  
                  substracted = substracted.substring(1);
                  substracted = substracted + dividend.charAt(indexNumber);
  
              }
  
              examinedP = substracted;
              //System.out.print(examinedP);
              substracted="";
              //System.out.println(" ");  System.out.println(" ");
              indexNumber++;
          }
  
  
          //counting remainder - if it starts with 0 - bye bye 0s..
          for (int i = 0;i&lt;examinedP.length();i++){
  
              if (Integer.parseInt(String.valueOf(examinedP.charAt(i)))==0){
                  substractZeros++;
              }
              else {
                  break;
              }
          }
  
          examinedP = examinedP.substring(substractZeros);
      //  System.out.println(" ");    System.out.println(" ");
      //  System.out.print(examinedP);
  
      return examinedP;
  
      }
  
      //XOR - Exclusive OR
      public int CRC_substract(int a, int b){
  
          if (a==0 &amp;&amp; b==0){
              return 0;
          }
          else if (a==0 &amp;&amp; b==1){
              return 1;
          }
          else if (a==1 &amp;&amp; b==0){
              return 1;
          }
          else if (a==1 &amp;&amp; b==1){
              return 0;
          }
  
          return -1;
      }
  
  
  
  }
  </code></pre>