View Single Post
#9
 cayoenrique Linux advisor Senior Member Join Date: May 2018 Posts: 500 Thanks: 319 Thanked 765 Times in 279 Posts Country:  Now lets look at one line

Code:
`ksfull = 0 ^ (((cw & 0x10) **** 4) | ((cw & 0x40) **** 5) | ((cw & 0x2) << 1) | ((cw & 0x1) << 3) | (cw & 0x10) | ((cw & 0x10) << 1) | (cw & 0x40) | ((cw & 0x20) << 2));`
This is take this template
Code:
`ksfull = 0 ^ ( ( eq1 ) | ( eq2 ) | ( eq3 ) | ( eq4 ) | ( eq5 ) | ( eq6 ) | ( eq7 ) | ( eq8 ) );`
See there 8 eq one for each bit in a byte. What this is doing is selecting a bit of the TOTAL 65 bit CW and shifting it around to another position.

So how we select bits on C. We do bitwise-and operation
Code:
`(cw & 0x10)`
this is of the form
Code:
`Byte & (bit position)`

So in the previous we are saying.
Byte is cw.
Bit position is 0x10.

lets see this in binary. Lets give value to cw= 0x8F

0x8F = 1000 1111
0x10 = 0001 0000

And we want to do and & And.
This means that to be 1 both bits has to have 1. If out bit selector ONLY have one bit with 1 as result we will optain the but value from it source.

0x8F = 1000 1111
&
0x10 = 0001 0000
-------------------
0x00 = 0000 0000

Now imagine original is cw = 0xFF
0xFF = 1111 1111
&
0x10 = 0001 0000
-------------------
0x01 = 0001 0000

Ahhaaa see now the result shows the value of the bit 5 position in cw. As result this is a bit select operation.

Now the original Idea is to move that bit to a new position.

(cw & 0x10) > > 4) where "> >" no space means me bit X positions to the right. To move to the left we do "<<".

Again if cw = 0xFF then we take our result and do "> >" 4

( 0001 0000 ) "> >" 4 = 0000 0001

See it is easy. We do a bitwise-and to select bit. Then we do a bitwise-move to shift bit to new position. We do 8 bitwise-or to finally add the result of all bits to construct our new byte.

Last is the symbol | . This is a bitwise-or operation. This is used to add all 8 bit that we move to end up with a new byte. Let me show you with two bits

0001 0000 | 0010 0000 = 0011 0000

Now lets do it with 8 bits

Code:
`0xAA = 1010 1010 = 0000 0000 | 0000 0010 | 0000 0000 | 0000 1000 | 0000 0000 | 0010 0000 | 0000 0000 | 1000 0000`
 The Following 2 Users Say Thank You to cayoenrique For This Useful Post: dvlajkovic (15-01-2022), Me2019H (15-01-2022)