CRC polynomial calculation - algorithm

I am trying to understand this document, but can't seem to get it right. http://www.ross.net/crc/download/crc_v3.txt
What's the algorithm used to calculate it?
I thought it uses XOR but I don't quite get it how he gets 0110 from 1100 XOR 1001. It should be 101 (or 0101 or 1010 if a bit goes down). If I can get this, I think the rest would come easy, but for some reason I just don't get it.
9= 1001 ) 0000011000010111 = 0617 = 1559 = DIVIDEND
DIVISOR 0000.,,....,.,,,
----.,,....,.,,,
0000,,....,.,,,
0000,,....,.,,,
----,,....,.,,,
0001,....,.,,,
0000,....,.,,,
----,....,.,,,
0011....,.,,,
0000....,.,,,
----....,.,,,
0110...,.,,,
0000...,.,,,
----...,.,,,
1100..,.,,,
1001..,.,,,
====..,.,,,
0110.,.,,,
0000.,.,,,
----.,.,,,
1100,.,,,
1001,.,,,
====,.,,,
0111.,,,
0000.,,,
----.,,,
1110,,,
1001,,,
====,,,
1011,,
1001,,
====,,
0101,
0000,
----
1011
1001
====
0010 = 02 = 2 = REMAINDER

The part you quoted is just standard long division like you learned in elementary school, except that it is done on binary numbers. At each step you perform a subtraction to get the remainder, and this is done in the example you gave: 1100 - 1001 = 0110.
Note that the article just uses this as a preliminary example, and it is not actually what is done in calculating CRC. Instead of normal numbers, CRC uses division of polynomials over the field GF(2). This can be modeled by using normal binary numbers and doing long division normally, except for using XOR instead of subtraction.

The link you provided says:
we'll do the division using good-'ol long division which you
learnt in school (remember?)
You just repetitively subtract, but since it is in binary, there are only two options: either the number fits once in the current selection, or 0 times. I annotated the steps:
0000011000010111
0000
1001 x 0
---- -
0000
1001 x 0
---- -
0001
1001 x 0
---- -
0011
1001 x 0
---- -
0110
1001 x 0
---- -
1100
1001 x 1
---- -
0110
1001 x 0
---- -
1100
1001 x 1
---- -
0110
and so on

Related

What sort of binary representation is %xA and such?

I'm setting up a websocket server to connect with a web browser, while reading the Specification (RFC 6455) for websocket frame exchange I came across these values that are supposed to represent a 4bit op code, they look like this:
%x0 %x1 %x2 ..... %xA %xB .... %xF
I know that %x0 = 0000 and %x1 = 0001
I'd like to know what these values are called and how to convert them to bits.
Thank you.
These %xN representations use a single hexadecimal digit to describe a four-bit binary number.
The possible values of a four-bit binary number range from 0 (0000) to 15 (1111), so each value can be expressed as a hexadecimal digit:
0 %x0
1 %x1
10 %x2
11 %x3
100 %x4
101 %x5
110 %x6
111 %x7
1000 %x8
1001 %x9
1010 %xA
1011 %xB
1100 %xC
1101 %xD
1110 %xE
1111 %xF

mips finding offset in beq instruction

I am new to MIPS - 32 and I have problem understanding the offset of the following instruction:
beq $a0,$a1,0x00401200
knowing that
PC=0x0040122C
I think that
$a0=00100
$a1=00101
the instruction should be 000100|00100|00101|0001 0010 0000 0000.
the solution says the offset is -12, but I do not understand why.
Could anyone give me a hand?
The target address is found by TA = PC + 4(offset)
So the offset = (TA - PC)/4
0x00401200 is your target address. We see that it is going backwards, so we must make sure our answer is negative. In this case, I prefer to do PC - TA and then find the 2s complement.
PC - TA : 0x0040122C - 0x00401200 = 0x002C
We can find the 2s complement by doing
0xFFFF(16)-0x002C = 0xFFFD4
Now we can divide by 4 to find the TA by doing a right shift 2 places
1111 1111 1111 1101 0100 -> 11 1111 1111 1111 0101
The offset = 0xFFF5
I ended up with the offset = -11 (0xFFF5). However, if I plug that back into
TA = PC + 4(offset), I get 0x401200.

How to bitmask a number (in hex) using the AND operator?

I know that you can bitmask by ANDing a value with 0. However, how can I both bitmask certain nibbles and maintain others. In other words if I have 0x000f0b7c and I wanted to mask the everything but b (in other words my result would be 0x00000b00) how would I use AND to do this? Would it require multiple steps?
You can better understand boolean operations if you represent values in binary form.
The AND operation between two binary digits returns 1 if both the binary digits have a value of 1, otherwise it returns 0.
Suppose you have two binary digits a and b, you can build the following "truth table":
a | b | a AND b
---+---+---------
0 | 0 | 0
1 | 0 | 0
0 | 1 | 0
1 | 1 | 1
The masking operation consists of ANDing a given value with a "mask" where every bit that needs to be preserved is set to 1, while every bit to discard is set to 0.
This is done by ANDing each bit of the given value with the corresponding bit of the mask.
The given value, 0xf0b7c, can be converted as follows:
f 0 b 7 c (hex)
1111 0000 1011 0111 1100 (bin)
If you want to preserve only the bits corresponding to the "b" value (bits 8..11) you can mask it this way:
f 0 b 7 c
1111 0000 1011 0111 1100
0000 0000 1111 0000 0000
The value 0000 0000 1111 0000 0000 can be converted to hex and has a value of 0xf00.
So if you calculate "0xf0b7c AND 0xf00" you obtain 0xb00.

Self complementing Codes

This statement was deemed true: Given any self-complementing decimal code scheme, if we know the codes for the number 283, then we can deduce the codes for 671.
I wanna know why. I took Excess-3 BCD as the self complementing code:
0-0011
1-0100
2-0101
3-0110
4-0111
5-1000
6-1001
7-1010
8-1011
9-1100
So 283 = 0101 1011 0110 .
671 = 1001 1010 0011
So why is the statement as it is as 283-ex3 is not a 1s complement of 671-ex3?
Since it is self-complementing decimal code scheme, then the code for 9's compliment of 283 can be obtained by taking 1's complement of code for 283.
9's complement of 283 = 716
283 = 0101 1011 0110. so its 1's complement = 1010 0100 1001 will be the code for 716.
From this: code for 7 =1010, that for 1 =0100 and for 6 = 1001
So code for 671 = 1001 1010 0100

How do you convert little Endian to big Endian with bitwise operations?

I get that you'd want to do something like take the first four bits put them on a stack (reading from left to right) then do you just put them in a register and shift them x times to put them at the right part of the number?
Something like
1000 0000 | 0000 0000 | 0000 0000 | 0000 1011
Stack: bottom - 1101 - top
shift it 28 times to the left
Then do something similar with the last four bits but shift to the right and store in a register.
Then you and that with an empty return value of 0
Is there an easier way?
Yes there is. Check out the _byteswap functions/intrinsics, and/or the bswap instruction.
You could do this way..
For example
I/p : 0010 1000 and i want output
1000 0010
input store into a variable x
int x;
i = x>>4
j = x<<4
k = i | j
print(K) //it will have 1000 0010.

Resources