All pastes #62324 Raw Edit

Miscellany

public text v1 · immutable
#62324 ·published 2006-06-05 23:21 UTC
rendered paste body
/* This is distributed under the GPL because I feel like it, okay? */
/*
 * (C) 2006 Philippe P. Vachon <philipva@nortel.com>
 * All Rights Reserved. This code probably doesn't work, so don't hold me responsible
 * if someone dies because of it, okay?
 */

#include <stdio.h>
#define CRC_ADDR_SIZE 5 

int calc_crc16_bit(int b, int crc16) {
        int temp;
        if ( ( (crc16 >> 15) ^ b ) & 1 ) {
                temp = crc16 << 1;
                temp ^= 0x8005;
        }
        else
                temp = crc16 << 1;
        return temp & 0xFFFF;
}

int calc_crc16(int addr, int data) {
        int temp = data;
        int crc16 = 0;
        int i;
        for (i = 0; i < 32; i++) {
                crc16 = calc_crc16_bit(temp,crc16);
                temp = temp >> 1;
        }
        temp = addr;
        for (i = 0; i < CRC_ADDR_SIZE - 1; i ++) {
                crc16 = calc_crc16_bit(temp,crc16);
                temp = temp >> 1;
        }
        return crc16;
}

int main() {
        int crc16;
        /* test this shiznit */
        crc16 = calc_crc16(0xDEAD, 0xBEEF);
        /* chirp chirp */
        printf("Result: 0x%x\n",crc16);
        return 0;
}