Applications in bit operation game development

Source: Internet
Author: User
Tags what bit

Applications in bit operation game development
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.