用于组合信用卡网络的位掩码枚举

时间:2015-07-22 02:27:13

标签: c enums bitmask

我正在尝试将卡提供商表示为enum。基本上信用卡(基于BIN号码)可以是Visa或Mastercard,子类型可以是Credit或Debit(简化示例,但我想处理所有情况(Maestro,Visa Electron,UATP ...)

它似乎有效,但看到最后一种情况,我们知道CardProvider Visa | CardProvider MasterCard可能不正确。

我有办法防止这种情况发生吗?除此之外,当前的enum结构是否正确?

enum CardProvider
{
    CardProviderNone             = 0,
    CardProviderMasterCard       = 1 << 0,
    CardProviderMasterCardDebit  = 1 << 1,
    CardProviderMasterCardCredit = 1 << 2,
    CardProviderVisa             = 2 << 0,
    CardProviderVisaDebit        = 2 << 1,
    CardProviderVisaCredit       = 2 << 2
};

CardProvider cardType1 = CardProviderMasterCard;
CardProvider cardType2 = CardProviderMasterCard | CardProviderMasterCardCredit;
CardProvider cardType3 = CardProviderMasterCard | CardProviderMasterCardDebit;
CardProvider cardType4 = CardProviderVisa | CardProviderVisaDebit;

// possible to prevent?
CardProvider cardType5 = CardProvider Visa | CardProvider MasterCard;

// works as expected:
assert(cardType1 & CardProviderMasterCard);
assert(cardType2 & CardProviderMasterCardCredit);
assert(cardType2 & CardProviderMasterCard);
assert(cardType3 & CardProviderMasterCard);
assert(!(cardType4 & CardProviderMasterCard));
assert(cardType4 & CardProviderVisa);

// works but shouldn't be allowed
assert(cardType5 & CardProviderVisa);

2 个答案:

答案 0 :(得分:1)

您可能需要为提供者和类型使用单独的枚举,并使用序号而不是位掩码:

enum CardProvider
{
    CardProviderNone             = 0,
    CardProviderMasterCard       = 1,
    CardProviderVisa             = 2
};

enum CardType
{
    CardTypeNone              = 0,
    CardTypeDebit             = 1,
    CardTypeCredit            = 2
};

答案 1 :(得分:0)

您的卡片类型重叠(2<<0 == 1<<1)您只需调整它们即可。有许多不同的方法,但这就是你想要做的事情。

typedef enum CardProvider {
    CardTypeNone                 = 0,
    CardTypeCredit               = 1<<0,
    CardTypeDebit                = 1<<1,
    /* you may want to add padding here for extra card types */

    CardProviderMasterCard       = 1<<2,
    CardProviderMasterCardDebit  = CardTypeDebit  | CardProviderMasterCard,
    CardProviderMasterCardCredit = CardTypeCredit | CardProviderMasterCard,

    CardProviderVisa             = 1<<3,
    CardProviderVisaDebit        = CardTypeDebit  | CardProviderVisa,
    CardProviderVisaCredit       = CardTypeCredit | CardProviderVisa,
    //future providers here
};

int card_transaction(CardProvider, float amount, struct card_data);

要在滥用时使编译器出错,请参阅https://stackoverflow.com/a/14822654/1162141