Qu

crc

Builtin index · Signal processing

result = crc(bits, polynomial)

Input: bits, polynomial · Output: result

What it is

bits: the message as a bit vector; polynomial: the generator, written MSB-first with its leading 1 included, either as a bit vector ([1,0,1,1]) or as the number spelling the same bits (11). Textbook unreflected, zero-initialized polynomial division: the message is shifted left by the CRC width and divided mod 2. The width comes from the polynomial, not from a hardcoded table, so any width works; this is deliberately not any one named catalogue variant (CRC-32 and friends also specify an init value, input/output reflection and a final XOR).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = crc(bits, polynomial)
bits vectorbits: the message as a bit vector.
polynomial vector or numberpolynomial: the generator, written MSB-first with its leading 1 included, either as a bit vector ([1,0,1,1]) or as the number spelling the same bits (11).
result Vec
Returns a Vec of 0/1, len(polynomial) - 1 bits long. (Added in v0.3.0.)

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

bits = [0, 1, 1, 0, 1, 0, 0, 1]
syms = qam_modulate(bits, 16)             # 2 symbols on the odd-integer grid

print(qam_demodulate(syms, 16))           # back to the same 8 bits


print(hamming74_encode([1, 0, 1, 1]))     # [0, 1, 1, 0, 0, 1, 1]

r = hamming74_decode([0, 1, 1, 0, 1, 1, 1])   # bit 5 flipped in transit

print("{r.data} corrected={r.corrected} at {r.positions}")

m = [1, 1, 0, 1]
print(crc(m, 11))                         # [0, 0, 1] -- x^3 + x + 1

print(crc_check([1, 1, 0, 1, 0, 0, 1], 11))   # true

output
[0, 1, 1, 0, 1, 0, 0, 1]
[0, 1, 1, 0, 0, 1, 1]
[1, 0, 1, 1] corrected=true at [5]
[0, 0, 1]
true

from signal-processing

How it works

crc(bits, polynomial) — the CRC remainder of bits under polynomial, returned as polynomial's width in 0/1 bits. The polynomial is data, not a hardcoded width: write it MSB-first with its leading 1, either as a bit vector ([1,0,1,1]) or as the number that spells (11).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.