| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
hpl3:community:scripting:classes:eflagbit [2015/11/06 03:16] abion47 [Remarks] |
hpl3:community:scripting:classes:eflagbit [2015/11/06 03:19] (current) abion47 [Remarks] |
||
|---|---|---|---|
| Line 53: | Line 53: | ||
| To check an integer for the presence of a flag, conversely you would use a [[wp>Bitwise_operation#AND|bitwise-AND]] operator. Because of how the bitwise-AND works, if you perform it on an integer and a flag, the result will be the value of the flag if the integer holds that flag, or it will be zero if not. | To check an integer for the presence of a flag, conversely you would use a [[wp>Bitwise_operation#AND|bitwise-AND]] operator. Because of how the bitwise-AND works, if you perform it on an integer and a flag, the result will be the value of the flag if the integer holds that flag, or it will be zero if not. | ||
| - | <code=c++>bool flagExists = (lFlags & eFlagBit_12) != 0;</code> | + | <code=c++>bool bContainsFlag = (lFlags & eFlagBit_12) != 0;</code> |
| + | |||
| + | There are also two utility flags called eFlagBit_None and eFlagBit_All, equal to 0 and -1 respectively. The way these flags work is that the None flag is set to the integer equivalent of all binary digits being 0, and the All flag has all binary digits set to 1 (again, this is due to the nature of [[wk>Signed_number_representations|signed integers]]). | ||
| Because bitwise operations are so fast and efficient, using a bit field in this way will result in much more efficient code than if you used standard booleans. The other upside is that a boolean variable only needs one bit to store its value (0 for false, 1 for true), but booleans still take up a full byte. Using a bit field, you can store up to 32 different flags in a single integer, meaning you are only using 4 bytes instead of 32. | Because bitwise operations are so fast and efficient, using a bit field in this way will result in much more efficient code than if you used standard booleans. The other upside is that a boolean variable only needs one bit to store its value (0 for false, 1 for true), but booleans still take up a full byte. Using a bit field, you can store up to 32 different flags in a single integer, meaning you are only using 4 bytes instead of 32. | ||