How would one go about storing positional information in bit fields (the order in which the fields are OR’d or otherwise)?
Background: It popped into my head last night while writing a part of my game engine. Let’s say that we are trying to describe a colour, and as part of that we have the colours that are present in the descriptor (and their order). For example we have the following colour orders on most graphics cards today:
RGBA
BGRA
The following flags can be used to describe colours that are supported:
None = 0x0
A = 0x1
R = 0x2
G = 0x4
B = 0x8
However, by using those fields A | R | G | B is the same thing as B | G | R | A. How would you design the flags and/or operations that can be used to add positional dependence? Bonus marks for adding exclusivity (you can’t have R and G in position 1, for example) and for utility (some clever way that it could be used, possibly in this case scenario).
You can shift the bit field before adding each flag, by the number of bits required for each unique flag. The following flags would be used:
On a little-endian system you would shift it left by
Shift(<<) before eachOR. The shift left onNonecan be eliminated because0 << x = 0. Given the original example:To extract the position of each you would repeatedly shift it right (little-endian) and
ANDit withMask. Repeating this until the current value reachesNonewould give you the reverse order.This does not offer exclusivity (you can easily do a AAGB) and it doesn’t look like it has any utility.