Voting

: max(one, six)?
(Example: nine)

The Note You're Voting On

same
21 years ago
bit by bit crc32 computation

<?php

function bitbybit_crc32($str,$first_call=false){

//reflection in 32 bits of crc32 polynomial 0x04C11DB7
$poly_reflected=0xEDB88320;

//=0xFFFFFFFF; //keep track of register value after each call
static $reg=0xFFFFFFFF;

//initialize register on first call
if($first_call) $reg=0xFFFFFFFF;

$n=strlen($str);
$zeros=$n<4 ? $n : 4;

//xor first $zeros=min(4,strlen($str)) bytes into the register
for($i=0;$i<$zeros;$i++)
$reg^=ord($str{$i})<<$i*8;

//now for the rest of the string
for($i=4;$i<$n;$i++){
$next_char=ord($str{$i});
for(
$j=0;$j<8;$j++)
$reg=(($reg>>1&0x7FFFFFFF)|($next_char>>$j&1)<<0x1F)
^(
$reg&1)*$poly_reflected;
}

//put in enough zeros at the end
for($i=0;$i<$zeros*8;$i++)
$reg=($reg>>1&0x7FFFFFFF)^($reg&1)*$poly_reflected;

//xor the register with 0xFFFFFFFF
return ~$reg;
}

$str="123456789"; //whatever
$blocksize=4; //whatever

for($i=0;$i<strlen($str);$i+=$blocksize) $crc=bitbybit_crc32(substr($str,$i,$blocksize),!$i);

?>

<< Back to user notes page

To Top