Enum Program 4

Description:

In this C++ example, we demonstrate how to use an enum to define a type that could be used to represent bit values.

Example

#include <iostream>

// We can define an enum to represent bits.
// We can use formulas and the previous values to assign new values.
// Here, we use the bit shift to set keZero = 1, keOne = 2, keTwo = 4.
enum EBit {keZero = 1, keOne = keZero << 1, keTwo = keOne << 1};

int main() {
    using namespace std;

    // Create an instance of the type and assign it the value keTwo.
    EBit eMyBit = keTwo;

    cout << "Bit enum value = " << eMyBit << endl;
    // Output the constant enum values.
    cout << "keZero = " << keZero << endl;
    cout << "keOne = " << keOne << endl;
    cout << "keTwo = " << keTwo << endl;

    // Keep the console window open.
    cin.get();
    return 0;
}

Output:

Enum Program 4 Output