I was wondering what your ideas were in developing an efficient algorithm for doing if/else switch/cases based on bits. I have 8 bits to play with and I need to divide them into higher order and lower order bits, like so:
0000 1111
Each half contains some information base on which bits are turned on. For example, if the lower half (1111 in this little endian machine) is actually 0010, something happens. Furthermore, if the higher end is 1000, something else happens.
I guess it would be efficient to rightshift the upper half and make AND comparisons (like (x >> 4) & 8) but I’m not sure what’s smart to do for the lower half, as it seems a bit unclever to left shift and compare to some weird number.
Your insights, again, greatly appreciated.
First of all, the
(x >> 4) & 8in your example isn’t quite right. To compare the higher nibble (the top four bits) ton, you need((x >> 4) & 15) == n.To compare the lower nibble to
n, you simply lose the right shift:(x & 15) == n.