Bit operation definition:
In layman's terms, bitwise operations are performed on binary numbers of an integer in a computer.
The principle of bit operations is very simple. Baidu encyclopedia can find some basic usage and related operators.
Most programmers who have just entered the gaming industry know what bit operations are, but they often do not remember to use them.
Any integer can be expressed in two-step progress mode. Different types of integers have different length digits. int8 or Char are represented by eight two-step progress bits, int16 or short is represented by 16 2 Progress bits, while int32 is represented by 32-bit status bits.
Application of bit operations in games
Often, when I perform bitwise operations in Game Development, I focus on whether the value of a certain digit is 0 or 1, rather than the value of this integer.
For example: 00100010, the eight-digit integer from right to left, its first digit is 0, the second digit is 1, the third digit is 0, and the sixth digit is 1.
How do I know the value of a certain digit of this integer? How do I change the value of a certain digit of an integer from 0 to 1 or from 1 to 0? See the following code:
// Check whether the POs position of the state is 1int bitcheck (INT state, int POS) {return state & 1 <pos-1 ;} // set the POs value of the state to 1int bitadd (INT state, int POS) {return state | (1 <(POS-1 ));} // set the POs value of the state to 0int bitdel (INT state, int POS) {return state &(~ (1 <(POS-1 )));}
Bit operations are often used in the game to record some States. A 32-bit integer can record 32 States, and only one int is enough.
Example:
Assume that an NPC has the following statuses: Walking status, standing status, normal Attack Status, and skill attack status. Depending on the status of the NPC, the client needs to play different actions.
A lot of students immediately think of enumeration, and then they started to write code, and soon the program was written.
At this time, the planning raised a new demand, saying that the attack may cause a burst attack. During the burst attack, both common attacks and skill attacks must have different performances.
Then the student thought for a while, and added a bool to record whether the attack had a blow, so he quickly wrote the code again.
Another requirement was raised during planning, and there was still a hit during the attack.
Therefore, a bool is added to record hits.
If there is still a buff effect, this will be a pitfall!
This is really what most of the newly recruited students do. One module has more than one million bool. The key is that these bool also uses an int8 method to send it to the client one by one.
If you have encountered such a problem, record the attack and brute-force attack status and the brute-force attack status into a new enumeration value.
Solution:
The status bit is used to record the status of the NPC. An 8-digit integer can record 8 States, 16 bits, and 32 bits can record more States.
// Pseudocode # define state_attack 1 // common attack # define state_skill 2 // skill attack # define state_dodge 3 // blow # define state_hit 4 // hit int8 state = 0; // No state by default, state = bitadd (State, state_attack); // initiate normal attack state = bitadd (State, state_hit); // hit target State = bitadd (state, state_dodge); // generate a burst if (bitcheck (State, state_hit) = 0) // if the target is not hit {}
The main difference between bitwise operations and enumeration-like operations is that bitwise operations can use one variable to record the coexistence of multiple States.
Bitwise operations are used in the game development field. For example, in the game reward system, there are many types of rewards in the game, such as daily logon, qualifying, arena, Guild rewards, and online rewards, sign-In rewards, etc. When you plan to receive each type of reward, the client needs light effects on the corresponding function buttons to attract Players' Attention, let the players know that a certain reward is available now. I have seen that many developers use the client to get all the system data, and then decide whether to enable the light effect based on the data. In fact, we only need to use an integer to determine whether each reward can be received on the server. After the client receives the data, it can enable the corresponding light effect based on each status, when the player clicks to enter the corresponding system, the corresponding data is obtained.
Applications in bit operation game development