为此,您可以使用标准枚举(在 C# 中)。为此,您需要设置FlagsAttribute,然后专门为这些值编号。代码看起来像这样:
[Flags]
public enum AvailableColours {
black = 1,
red = 2,
green = 4,
blue = 8,
white = 16,
}
然后,标准的按位运算符将按预期工作。
[编辑] 嗯,好的,你想生成可能的组合,对吧?您的要求非常具体,所以如果有任何工具接近您想要的,我会感到非常惊讶。我认为您将不得不自己动手。我假设你想要这些作为字符串,对吗?这是一些实用程序代码,至少可以帮助您入门:
public const int BITS_IN_BYTE = 8;
public const int BYTES_IN_INT = sizeof(int);
public const int BITS_IN_INT = BYTES_IN_INT * BITS_IN_BYTE;
/// <summary>
/// Display the bits in an integer
/// </summary>
/// <param name="intToDisplay">The integer to display</param>
/// <returns>A string representation of the bits</returns>
public string IntToBitString(int intToDisplay) {
StringBuilder sb = new StringBuilder();
AppendBitString(intToDisplay, sb);
return sb.ToString();
}
/// <summary>
/// Displays the bits in an integer array
/// </summary>
/// <param name="intsToDisplay">Arrau to display</param>
/// <returns>String representation of the bits</returns>
public string IntArrayToBitString(int[] intsToDisplay) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < intsToDisplay.Length -1; i++) {
AppendBitString(intsToDisplay[i], sb);
sb.Append(' ');
}
if (intsToDisplay.Length - 1 > 0)
AppendBitString(intsToDisplay[intsToDisplay.Length - 1], sb);
return sb.ToString();
}
private void AppendBitString(int intToAppend, StringBuilder sb) {
for (int j = BITS_IN_INT - 1; j >= 0; j--) {
sb.Append((intToAppend >> j) & 1);
if (j % 4 == 0 && j > 1)
sb.Append(' ');
}
}
/// <summary>
/// Creates an integer from a bit string. This method can be used
/// to explicitly set bits in an integer during testing.
/// </summary>
/// <example>
/// int i = bitUtil.IntFromBitString("0000 0000 0000 0100");
/// </example>
/// <param name="bitString">String representing the individual bits</param>
/// <returns></returns>
public int IntFromBitString(String bitString) {
int returnInt = 0;
int currentBitPos = bitString.Length;
for (int i = bitString.Length - 1; i >= 0; i--) {
char c = bitString[i];
if (Char.IsWhiteSpace(c)) continue;
if (c == '1') {
returnInt |= 1 << (bitString.Length - currentBitPos);
}
currentBitPos--;
}
return returnInt;
}
/// <summary>
/// Tests the status of an individual bit in and integer. It is 0 based starting from the most
/// significant bit.
/// </summary>
/// <param name="bits">The integer to test</param>
/// <param name="pos">The position we're interested in</param>
/// <returns>True if the bit is set, false otherwise</returns>
public bool IsBitOn(int bits, int pos) {
int shiftAmnt = (BITS_IN_INT - 1) - pos;
return ((bits >> shiftAmnt) & 1) == 1;
}
/// <summary>
/// Calculates the number of integers (as in an array of ints) required to
/// store a given number of bits
/// </summary>
/// <param name="bitsNeeded">The total count of required bits</param>
/// <returns>The number of integers required to represent a given bit count</returns>
public int RequiredSizeOfIntArray(int bitsNeeded) {
return (bitsNeeded / BITS_IN_INT) + (((bitsNeeded % BITS_IN_INT) == 0) ? 0 : 1);
}
/// <summary>
/// Calculates which array element would hold the individual bit for a given bit position
/// </summary>
/// <param name="bitPos">The position of the interesting bit</param>
/// <returns>An index into an array of integers</returns>
public int ArrayPositionForBit(int bitPos) {
return bitPos / BITS_IN_INT;
}
/// <summary>
/// Sets an individual bit to a given value
/// </summary>
/// <param name="bits">The integer containing the bits</param>
/// <param name="pos">The position in the integer to set</param>
/// <param name="isSet">True for on, False for off</param>
public void SetBit(ref int bits, int pos, bool isSet) {
int posToSet = (BITS_IN_INT - 1) - pos;
if (isSet)
bits |= 1 << posToSet;
else
bits &= ~(1 << posToSet);
}
/// <summary>
/// Converts an array of integers into a comma seperated list
/// of hexidecimal values.
/// </summary>
/// <param name="bits">The array of integers</param>
/// <returns>String format</returns>
public String IntArrayToHexString(int[] bits) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bits.Length - 1; i++) {
sb.Append(bits[i].ToString("X"));
sb.Append(',');
}
if (bits.Length > 0) {
sb.Append(bits[bits.Length - 1].ToString("X"));
}
return sb.ToString();
}
/// <summary>
/// Parses a comma seperated list of hexidecimal values and
/// returns an array of integers for those values
/// </summary>
/// <param name="hexString">Comma seperated hex values</param>
/// <returns>integer array</returns>
public int[] HexStringToIntArray(String hexString) {
string[] hexVals = hexString.Split(new char[] {','});
int[] retInts = new int[hexVals.Length];
for (int i = 0; i < hexVals.Length; i++) {
retInts[i] = Int32.Parse(hexVals[i], System.Globalization.NumberStyles.HexNumber);
}
return retInts;
}