View previous topic :: View next topic |
Author |
Message |
iseron
Joined: 21 Feb 2004 Posts: 12 Location: Santiago - Chile
|
make a byte between 2 other bytes |
Posted: Thu Mar 04, 2004 2:37 pm |
|
|
Hi.
i need to create a byte, but from the half of other 2 bytes.
This is for implementig a FAT12 routine, FAt 12 mean that it use only 12 bits. Then i will write this on a Compact Flash.
Well, now i have a counter to a max number of 4095= FFFh. And i need to make this composition using 3 bytes = 24 bits then dividing the byte in the half between the numbers.
Its better an example i think.
Example: For implementing the 12 bits, i have to produce this numbers --> 001 002 003.... FFD FFE FFF; and my problem es because i can only write bytes blocks to save into the compact flash 00 10 02 00 3X XX........XX XF FD FF EF FF
So, now i'm "cuting & paste" the XXX numbers into a XXh XXh but i can't know how to do that.
Now i'm Using this algorithm, but don't know....
notice this --> unsigned long int k; k++
1) write (k>>8))
2) write (k+1)>>4 & 0xFF) & (k<<4 & 0xFF)
3) write (k+1 & 0xFF))
4)k++
ONE QUESTION:
The "& " is for paste the bytes?
I'm very lost with this..:-( |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Mar 04, 2004 3:05 pm |
|
|
If I understand correctly you wish to store 12 bit words in sets using 3 bytes per set. I would use a byte for the lower half of each word and use the remaining byte to store the upper nibbles.
word_1 = ((Byte3&0x0F)<<8) + Byte1;
word_2 = (((swap(Byte3))&0x0F)<<8) + Byte2; |
|
![](templates/subSilver/images/spacer.gif) |
SteveS
Joined: 27 Oct 2003 Posts: 126
|
|
Posted: Thu Mar 04, 2004 3:14 pm |
|
|
Not sure I understand exactly, but let me try an answer:
First:
'&' is a bit-wise AND operator - it will produce a result that has a '1' in each bit location only if both inputs have a '1' at that location, so (in binary)
0101 & 0011 = 0001
'|' (vertical bar) is the bit-wise OR operator - it produces a '1' in the result
each bit of the result if either input has a '1' at that bit:
0101 | 0011 = 0111
That may be the 'paste' operation you are thinking of.
Ok, now if you want to turn (hex now) 001 002 003 ... into 00 10 02 00 3...
I think you have the right idea, but a couple corrections
1) write (k>>4)) // not >> 8 to get rid of the lower 4 bits
2) write (k+1)>>4 & 0xFF) | (k<<4 & 0xFF) // | not & to combine the pieces
3) write (k+1 & 0xFF)) // ok
4)k += 2; // since each loop actually takes care of two 12 bit values.
SteveS |
|
![](templates/subSilver/images/spacer.gif) |
iseron
Joined: 21 Feb 2004 Posts: 12 Location: Santiago - Chile
|
|
Posted: Sat Mar 06, 2004 10:33 pm |
|
|
Thanks!!
Excelent Guide to finish this part of my algorithm.
The given code was not exactly what was i looked for but. i learn it!
Really thanks!
Ignacio. |
|
![](templates/subSilver/images/spacer.gif) |
|