I have a question relating to bit reordering in a byte / word or even long word.
I have seen algorithms to swap one value for another by using XOR technique, and also algorithms to swap bits in the byte or word from lowest to highest and vice versa (reversing the bit pattern).
What I want to achieve is the ability to reorder the bits in a byte or word (and possibly long word) so that each bit can have a new defined location (as expressed by the user) but each bit maps uniquely to a new or the same location in the byte - with no double assignments.
To give you some background - I am interested in providing something similar to the QEP III's address and data bit reordering.
I was just curious to know if there was any technique that might make this more optimized - as at the moment I can only envisage running through a loop to process each bit in the byte or word and use shifts / rotates to move the bits up or down as required, whilst maintaining the previous movements made so far - it seems kludgy at best...
Do the shuffle...
-
- Super Gold Card
- Posts: 716
- Joined: Mon Nov 24, 2014 2:03 pm
Re: Do the shuffle...
Hi Prof,
What sort of language are you using for bit-manipulation ?
In SBasic it is very convenient to do prototyping using pseudo-binary string$ of any length.
These can then easily be reconverted back to the desired types using keywords (suppiled as EXTRAs).
Once you have finalised your algorythms, use bitwise operators for speed...
Steve.
_____________
What sort of language are you using for bit-manipulation ?
In SBasic it is very convenient to do prototyping using pseudo-binary string$ of any length.
These can then easily be reconverted back to the desired types using keywords (suppiled as EXTRAs).
Once you have finalised your algorythms, use bitwise operators for speed...
Steve.
_____________
Re: Do the shuffle...
I was probably going to go for assembler as I'm a glutton for punishment
for the 8 bit value - I reasoned that I would be able to loop around 8 times, using the new value of the bit assignment as a mask and decide whether to AND or OR the bit to set it's value based on the original value. I was wondering if there was some sort of function that could be built based on the bit swap's which could then be applied to the original value to create the new bit ordered value, as the process would have to be run for every byte in an Eprom - and for larger eprom's that would be quite a lot of work - as address (word value) and data (byte value) would both need to have the translation done. The only constant is the actual bit reordering is consistent for the data and the address values for all the values in the range, although address and data are seperate translations
for the 8 bit value - I reasoned that I would be able to loop around 8 times, using the new value of the bit assignment as a mask and decide whether to AND or OR the bit to set it's value based on the original value. I was wondering if there was some sort of function that could be built based on the bit swap's which could then be applied to the original value to create the new bit ordered value, as the process would have to be run for every byte in an Eprom - and for larger eprom's that would be quite a lot of work - as address (word value) and data (byte value) would both need to have the translation done. The only constant is the actual bit reordering is consistent for the data and the address values for all the values in the range, although address and data are seperate translations
Re: Do the shuffle...
If you want to do arbitrary bit swaps, that's a bit tricky, especially if you want to use logical functions. I would probably rather go with BTST and BSET there.
Something along the line of the following would work (save the registers you want preserved yourselves, and no, I didn't test it):
That simply loops over the source, tests each bit if its set, and if yes, sets the corresponding bit in the destination. The corresponding bit is given in a simple byte table. (Note because the destination register is set to zero before, there's no need to clear bits)
Something along the line of the following would work (save the registers you want preserved yourselves, and no, I didn't test it):
Code: Select all
tgt: dc.b 1,0,4,3,2,7,6,5 ; where do bits 0-7 go? (i.e. bit 0 moves to bit 1, bit 1 to 0, 2 to 4,....)
; d0.b: number to bitswap
bitswap:
move.w #8-1,d7 ; loop counter
moveq #0,d6 ; index into tgt
move.l d6,d4
lea tgt(pc),a0 ; pointer to table
move.b d6,d5 ; temporary result, all bits cleared
loop:
move.b 0(a0,d6.w),d4 ; target bit number for the d6-th bit in the source
btst d6,d0
beq noSet
bset d4,d5
noSet:
addq.w #1,d6
dbra d7,loop
move.b d5,d0 ; return bit-swapped register
rts
ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO