![]() In your example, you are rotating bits 1-5 inclusive, by one bit to the left: 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 The "moving a bit" operation that you describe is really a rotation of a contiguous range of bits. I realise that a precompiled lookup table could be built for a byte, but if this is to be extended to integers/longs, this would be impractical for me.įirst, an observation about the original problem, and the subsequent extensions that you mention: However, while the theory seems good, an elegant implementation is beyond me. My own niaive approach would be to identify the source and target bit positions, decide if shift up or down, take a shifted copy, mask off the static bits and find the source bit, merge the static and shifted bits and somehow set/clear the target bit. Performance is not a major issue, but something elegant is likley to be plenty fast enough. To more than one bit at a time, probably adjacent bits if easier) Possibly extendable from byte to unsigned int, long, etc.Implies shift down, 3>7 shift up or start-bit +/- offset: 6-4 or 3+4, or bit weighted: bit 6=64 to bit 3=8. ![]() Given a bit number, based on 0-7 Least-sig-bit to most-sig-bit, (or bits 1-8 if you'd rather), I would like to move a bit from one position to another: 7654 3210 2 For simplicity, lets use a simple 8-bit byte and just one bit to move within the byte. Is there an elegant way of moving a bit within a byte (or word/long).
0 Comments
Leave a Reply. |