APPENDIX 3

image

Bitwise Flags

As described in Chapter 1, you can use an enumeration to define bit flags. Bit flags allow a series of items to be selected or deselected by switching individual bits in a sequence on and off. To ensure that each value in an enumeration relates to a single bit, the numbering must follow the binary sequence whereby each value is a power of two, for example

1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, and so on

Listing A3-1 is a copy of the example from Chapter 1 and shows bit flags in action. Appendix 3 describes bit flags and bitwise operations in more detail.

Listing A3-1. Flags

enum DiscFlags {
        None = 0,
        Drive = 1,
        Influence = 2,
        Steadiness = 4,
        Conscientiousness = 8
}

// Using flags
var personality = DiscFlags.Drive | DiscFlags.Conscientiousness;

// Testing flags

// true
var hasD = (personality & DiscFlags.Drive) == DiscFlags.Drive;

// false
var hasI = (personality & DiscFlags.Influence) == DiscFlags.Influence;

// false
var hasS = (personality & DiscFlags.Steadiness) == DiscFlags.Steadiness;

// true
var hasC = (personality & DiscFlags.Conscientiousness) == DiscFlags.Conscientiousness;

Bit Flags Explained

The flags in Listing A3-1 are illustrated in Table A3-1. Because the bit flags are represented as 32-bit integers in big-endian order, the bit representing the greatest value appears on the left. To show the values in the order they are represented, "Conscientiousness" is shown on the left, because it is represented by the bit with the greatest value in the example: 8.

Table A3-1. Bit flags illustrated

TableA3-1.jpg

If the flag is switched on, the column shows a 1. If the flag is switched off the column shows a 0. The binary value shows that each digit in the binary representation corresponds to a value in the enumeration; whenever “Drive” is switched on, the right-most column will contain a 1. This is the reason the value of an enumeration being used for flags must use the binary sequence as it is the only sequence that means each flag will only affect a single bit in the set.

Bitwise Operations

Using bit flags also means you can use bitwise operators to manipulate the bits representing the items in the enumeration. Listing A3-2 uses a bitwise AND operator (&) to find matching traits in two sets of flags. Only items that are switched on in both sets of flags are switched on in the result of the bitwise AND operation, this is why a bitwise AND is used to see if an individual value is switched on.

Listing A3-2. Finding matches with a bitwise AND

// Both personalityA and personalityB include DiscFlags.Influence
var personalityA = DiscFlags.Drive | DiscFlags.Influence | DiscFlags.Conscientiousness;
var personalityB = DiscFlags.Influence | DiscFlags.Steadiness;

// The result of a bitwise AND contains only matching flags

// DiscFlags.Influence
var matchingTraits = personalityA & personalityB;

The results of various bitwise operations are illustrated in Table A3-2. They produce the following results:

  • AND results in bits being switched on if both sets have the bit switched on.
  • OR results in bits being switched on if either, or both, of the sets have the bit switched on.
  • XOR results in bits being switched on if one of the sets has the bit switched on. The bit is switched off if neither of the sources has the bit switched on or if both of the sources has the bit switched on.
  • NOT inverts a set, switching off all of the “on” bits and switching on all of the “off” bits.

Table A3-2. Bitwise operations

TableA3-2.jpg

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.20.20