Messages in this thread | | | Date | Wed, 10 Dec 1997 10:27:50 -0500 (EST) | From | Michael Poole <> | Subject | Re: copy-bit macro |
| |
On Tue, 9 Dec 1997, Stephen Williams wrote: [I believe this first part was written by Colin Plumb]
> if (input & INPUT_FLAG_FOO) > output |= OUTPUT_FLAG_FOO; > if (input & INPUT_FLAG_BAR) > output |= OUTPUT_FLAG_BAR > if (input & INPUT_FLAG_BAZ) > output |= OUTPUT_FLAG_BAZ > etc. > > output |= ((input / INPUT_FLAG_FOO) & 1) * OUTPUT_FLAG_FOO; > output |= ((input / INPUT_FLAG_BAR) & 1) * OUTPUT_FLAG_BAR; > output |= ((input / INPUT_FLAG_BAZ) & 1) * OUTPUT_FLAG_BAZ;
This only works if each of the input flags have only one bit set. That's almost always going to be the case, but some code, somewhere, sometime, might use a multi-bit input flag and then you'd have to convert it to something like:
output |= ((input / INPUT_FLAG_FOO_1) & 1) * ((input / INPUT_FLAG_FOO_2) & 1) * OUTPUT_FLAG_FOO;
> Seems to me, this specific example can be reduced to > output |= input & (INPUT_FLAG_FOO|INPUT_FLAG_BAR|INPUT_FLAG_BAZ); > > A single bit copy can be: > output |= input & INPUT_FLAG_FOO; > > If people are really writing codes like the if statements above, then > something is wrong. I suspect the example has been stripped down a bit. > If not, I think it is better to use the "x |= i & FLAG" if that really > is the case as yet another macro can be avoided.
I think your example is the stripped-down one; you assume that INPUT_FLAG_XXX == OUTPUT_FLAG_XXX. Presumably they are different, and people *would* use the shorter code you suggested if the input and output flags were identical.
- Michael
| |