Bill B said:
Trying to figure out an easy way to update a single bit within a group
of 24 bits (3 bytes) using a lookup table that returns 8 bits. The
first 5 bits from the table will indicate the bit to be updated (1of
24) and the 6th bit will indicate a set or reset for that particular
bit.
It's called math:
call your first 5 bits the index, the 6th bit the value, the 8-bits a and
the 24-bits stream:
then it's
(stream & ~(1 << (a && 63))) | ((((a >> 6) & 1) << a && 63)
or broken down
value = (a >> 6) & 1;
This shifts the 6th bit to the first 1 and then ands it with 1 so that the
higher bits go to 0.
index = a && 011111b;
this gets teh index, which is just the first 5 bits. i.e., I set the top 3
bits to 0 and so I'm just left with the lower 5 having any effect.
but index represents the shifting amount. i.e., if index = 8 then I shift 8
bits...
i.e.,
value << index
This basically takes the value and puts it at the indexth bit location.
Now we just need to update the stream(the 24-bits) with our new value
(1 << (a && 63))
What this does is create create a mask where every bit is 0 except for the
bit at the index we want.
We invert it to create an and mask... so went we and it with stream we
"punch" out that bit location so we can or in the true value.
stream & (1 << (a && 63))
makes the indexth bit location 0, oring this with our new value put's in it.
(this assumes you have no routine to set bit's directly)
Thought about using a counter and 3 other registers to shift the bit
through the 3 registers so the single bit appears in the correct
place, and then do an inclusive or to mask the bit into the existing
data. but it seems complicated.
Have any other ideas?
Not sure what you are talking about here. Bit manipulation can almost always
be done using boolean logic. You just have to use the standard tricks and
know how the operators work.
heres a concrete example for what is above: (I'm using low bit first and
only a trimmed down version)
suppose stream = 0110101 (7bits, not 24)
value = 110x (lower 3 bits = index, 4th bit = value)
then
index = a & 111b = 110 = 3
value = a >> 3 = x
1 << index = 0001
~(1 << index) = 1110111
stream & 1110111 = 0110101 (Didn't change)
value << index = 000x
so
(stream & 1110111) | (value << index)
=
0110101 | 000x000 = 011x101
So I have moved the x to the proper location in the bit stream.