definition of bit operation: In layman's point, a bitwise operation is a binary operation of an integer in a computer.
Any integer can be expressed in two ways, the number of different types of integers, the length of its bits is not the same, INT8 or char is made up of 8 2 progress
Bits indicate that INT16 or short is represented by 16 2 progress bits, INT32 is represented by a 32-bit status bit.
The application of bit operation in gameoften, when doing bit arithmetic in game development, my main concern is whether the value of a bit is 0, or 1, and not the value of the whole number.
For example: 00100010, the 8-bit integer from right to left, its first bit is 0, the second bit is 1, the third is 0, the sixth bit is 1.
Check if the POS bit of state is 1int bitcheck (int state, int pos) { return state & 1<< pos-1;}//Set the value of POS bit of state to 1int bit ADD (int state, int pos) { return state | (1 << (pos-1));} Set the value of the POS bit of state to 0int Bitdel (int state, int pos) { return state & (~ (1 << (pos-1)));}
bit arithmetic is often used in the game to record some states, a 32-bit integer, you can record 32 states, and only need an int is enough.
Example:
Suppose an NPC has the following States, walking state, standing state, normal attack state, skill attack state, and depending on the state of the NPC, the client needs to play different actions. Many students immediately think of the enumeration, and then began to write code, quickly put the program written. This time, the plan has also proposed a new demand, said that the attack may produce the crit, the common attack and the skill attack both need to have the different performance when the Crit strikes.
Then the classmate thought about it, and then added a bool to record whether the attack caused a crit, and then quickly wrote the code. Planning to ask for a demand, when the attack still have hit. Then a bool was added to record the hit. If there is a buff effect, this is the pit daddy!
It is true that most of the newly recruited students are doing so, a module of multiple bool, the key is very irritating is that these bool also use a INT8 way to send to the client.
Solution:
Use the status bit to record the state of the NPC, a 8-bit integer can record 8 states, 16-bit, 32-bit can record more states
Pseudo code # # state_attack 1/Common attack # State_skill 2//Skill Attack # State_dodge 3 //Crit # # State_hit 4 //Hit INT8 state = 0; Default no state = Bitadd (State,state_attack); Launch common Attack state = Bitadd (state,state_hit); Hit Target State = Bitadd (State,state_dodge); Generate Crit if (Bitcheck (state,state_hit) = = 0)//if Target {} is not hit
The
main difference between bit operations and enumeration-like operations is that bit operations can use a variable to record a situation in which multiple states exist together.
bit operations in the game development field is used in a lot of, such as in the game's reward system , the game has a lot of rewards, daily login, qualifying, Arena, guild Awards, online awards, sign-in awards, etc., planning needs each kind of reward can be picked up, The client in the corresponding function button all need light performance to arouse the attention of the player, let the player know that a reward is now can be collected.
I see a lot of developers are the client to get all the data of the system, and then according to the data of the corresponding situation to decide whether to let this button to turn on the light effect. In fact, we only need to use an integer, on the server side to calculate whether each kind of reward can be picked up, the client received this data, according to each state of the situation to open the corresponding light effect, so that players click into the corresponding system , only to get the corresponding data.
Bit operations are commonly used for status bits, permission bits, custom encryption
Application of & bit operation symbol
Clear 0: Quickly to a piece of data unit data zeroing, will be all of its bits is 0. For example, an integer number a=321 the operation of zeroing all its data to a=a&0x0. (321=0000 0001 0100 0001) &0=0000 0000 0000 0000= 0000 0000 0000 0000
- Gets the specified bit of a data
For example, the operation of getting the low eight-bit data for an integer number a=321 is A=a&0xff. (321=0000 0001 0100 0001) & (0xFF = 0000 0000 1111 11111)
= 0000 0000 0100 0001
The operation for obtaining the high eight-bit data of an integer number a=321 is a=a&0xff00. (321=0000 0001 0100 0001) & (0xff00=1111 1111 0000 0000)
= 0000 0001 0000 0000
- Keep a specific bit of the data area
For example, a data operation that obtains the 第7-8位 (starting at 0) bit of an integer number a=321 is: a=a&110000000
(321=0000 0001 0100 0001) & (384=0000 0001 + 0000) =0000 0001 0000 0000
Application of [C + + basic] bit arithmetic game development