User Tools

Site Tools


hpl3:community:scripting:classes:eflagbit

Link to this comparison view

Next revision
Previous revision
hpl3:community:scripting:classes:eflagbit [2015/11/05 12:23]
abion47 created
hpl3:community:scripting:classes:eflagbit [2015/11/06 03:19] (current)
abion47 [Remarks]
Line 41: Line 41:
 ====Remarks==== ====Remarks====
  
-Have some helpful descriptions to add to this class? Edit this page and add your insight ​to the Wiki!+A flag bit is a specialized form of storing a number of boolean flags within a single integer value. This is a form of data storage that is known as a [[wp>​Bit_Field|Bit Field]]. How it works is that, rather than store values within separate boolean fields, you instead leverage bit-shift operations ​to use the individual bits of an integer.
  
 +First, notice that each of the above values represent a certain power of two, going from 2<​sup>​0</​sup>​ (1) to 2<​sup>​3</​sup>​ (1073741824). The value for 2<​sup>​31</​sup>​ is also included, but due to the nature of [[wk>​Signed_number_representations|signed integers]], its value is -2147483648. Each of these flags represents a single 1 in the integer'​s binary value. For example, the binary value of 2<​sup>​5</​sup>,​ or 32, in an integer would be 00000000000000000000000000100000 (a 1 in the sixth position).
 +
 +Because of this, if you take different flags and add them together, they will result in a value that represents a 1 in each of those flag's position. For example, adding together eFlagBit_2, eFlagBit_7, eFlagBit_12,​ and eFlagBit_22 will result an integer that has 1's in the third, eighth, thirteenth, and twenty-third position. (00000000010000000001000010000100,​ or 4,198,532.)
 +
 +In code, the way to combine different flags into a single value is pretty simple. All you do is use the [[wp>​Bitwise_operation#​OR|bitwise-OR]] operator to combine each desired flag into a single value.
 +
 +<​code=c++>​int lFlags = eFlagBit_2 | eFlagBit_7 | eFlagBit_12 | eFlagBit_22;</​code>​
 +
 +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 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.
hpl3/community/scripting/classes/eflagbit.1446726224.txt.gz ยท Last modified: 2015/11/05 12:23 by abion47