#!/usr/bin/perl
#
# CRC16 algorythm implimented by Sheer

# (there's probably already a PM that does this)
#

@test_array = (1);

printf("CRC: %x\n",crc_16(@test_array));

sub crc_16 {
	my $crcval, $n, $q, $c, $t1, $t2;
	$crcval = 0;

	while((defined($c = shift))) {
		$t1 = ($crcval ^ $c);
		$q = $t1 & 0x000f;

		$t1 = ($crcval >> 4);
		$t2 = $q * 0x1081;
		$crcval = $t1 ^ $t2;

		$t1 = ($c >> 4);
		$t2 = ($crcval ^ $t1);
		$q = ($t2 & 0x000f);

		$t1 = ($crcval >> 4);
		$t2 = $q * 0x1081;
		$crcval = $t1 ^ $t2;
	}

	return $crcval;

}

