Math.java :  » UnTagged » moca » org » moca » util » Android Open Source

Android Open Source » UnTagged » moca 
moca » org » moca » util » Math.java
package org.moca.util;

public class Math {

  public static int crc16_ccitt(byte[] buffer, int offset, int size) {
    int crc = 0xFFFF; // Start value is 0xFFFF
    int poly = 0x1021; // Standard CCITT polynomial
    
    for (int j = 0; j < size; j++) {
      byte b = buffer[offset + j]; 
      for (int i = 0; i < 8; i++) {
        boolean bit = ((b   >> (7-i) & 1) == 1);
                boolean c15 = ((crc >> 15    & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit) crc ^= poly;
      }
    }
    return crc & 0xffff;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.