Reverse bits of a given the unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represent Ed in binary as 00111001011110000010100101000000).
Follow up:
If This function is called many times, what would you optimize it?
Solution:
Basically, this code is just keeping pops the last bit from N and push it to the end of the return result.
Do
? Get last bit from n
? Push the bit to the end of the result
? Pop out the last bit of n
Until N is 0
? Push remaining 0s to the n by "ret << nshift"
? Return the result "ret"
uint32_t ret =0;intNshift = +; while(n && nshift--) {// ShiftRET to left by one andMove a forThe newPushret = (ret <<1);//Push the LastBit of the N to RETif(n%2) RET |=0x1;// PopThe LastElement out n = (n>>1); }returnRET << Nshift;
Bit operation detailed
Reference: http://www.crazycpp.com/?p=82
Let's take a look at the bitwise operator:& (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise reversed), >> (bitwise right SHIFT), << (bitwise left shift).
1, & (bitwise AND) Conceptually, is to participate in the operation of each of the two components to do the logic and operation, if both are true (equals 1), then the result is true (equals 1). Otherwise they are false (equal to 0).
i.e.: 1 & 1 = 1, 1&0 = 0, 0&1 = 1, 0&0 = 0
Let's take a look at the example of a 8-bit binary in the first place:
7&8 = 0000 0111 & 0000 1000 = 0000 0000 = 0
7&6 = 0000 0111 & 0000 0110 = 0000 0110 = 6
2, | (bitwise OR) is the logic or operation of each of the components that participate in the operation, that is, when both are false (0), it is False (0), otherwise they are true.
namely: 0|0 = 0, 1|0 = 1, 0|1 = 1, 1|1 = 1
Take a look at the 8-bit binary example:
7|8 = 0000 0111 | 0000 1000 = 0000 1111 = 15
7|6 = 0000 0111 | 0000 0110 = 0000 0111 = 7
3, ^ (bitwise XOR) is the participation of each component of the operation to do the XOR or operation, that is, the same is false, the difference is true.
namely: 0|0 = 0, 1|0 = 1, 0|1 = 1, 1|1 = 0
Look at the following example:
7^8 = 0000 0111 ^ 0000 1000 = 0000 0111 = 7
7^6 = 0000 0111 ^ 0000 0100 = 0000 0011 = 3
4, ~ (bitwise negation) is the bits each to take the inverse operation, in short, 1 becomes 0, 0 becomes 1.
See examples directly:
~0000 = 0111 = 1111 1000 = 248
5 >> (Shift right) move the bits whole to the right.
7>>1 = 0000 0111 >> 1 = 0000 0011 = 3
7>>2 = 0000 0111 >> 2 = 0000 0001 = 1
The right shift here is equal to the number of n times except 2, and N is the right shift.
6 << (bitwise left) here is not the details, and the right shift opposite.
-Bit Operation application
Well, let's talk about practical applications.
One or one colors represented by a DWORD to represent the color
Definition: typedef unsigned long DWORD;
That is, an unsigned 32-bit (32-machine) long integer, with four bytes, we call him 1,2,3,4 bytes from left to right, and the range of each byte is 0~255. The first byte represents an alpha value, which is transparency. If it is 255, it is opaque and 0 is completely transparent (
Not visible), the other is the R,g,b value, respectively.
The values for each byte can be obtained in the following ways:
int A = (int) ((DWORD & 0xff000000) >> 24);
int R = (int) ((DWORD & 0x00ff0000) >> 16);
int G = (int) ((DWORD & 0x0000FF00) >> 8);
int B = (int) (DWORD & 0X000000FF);
DWORD Dwcolor = (a<<24) + (r<<16) + (g<<8) +b;
With the basis of the previous, I believe that everyone on the above conversion method, one can see it. If you are not sensitive to 16 of children's shoes, you can use the computer to convert hexadecimal into binary, easier to understand.
Second, the use of the State system
In game development, we typically use a 32-bit integer (assuming this is a 32-bit) to store the state of the role (this is done primarily to conserve storage space and also to reduce the size of the network synchronization message packet). The so-called state, is familiar with the buff or debuff.
Enum Role_status
{
Status_normal = 0,//Normal
Status_die = 1,//death status
Status_god,//Invincible
Status_disappearing,//Vanishing State
Status_def_adjust,//Physical Defense Promotion/reduction
Status_mdef_adjust,//mana Defense boost/decrease
Status_atk_cri_adjust,//simultaneously improve physical attack and crit rate
Status_maxhp_adjust,//HP Cap adjustment
Status_maxmp_adjust,//MP cap boost/decrease
......
You can write up to 32 at most, because we assume that 32 bits of data are used to store the state.
};
The status data is defined, so let's look at how to use them.
First of all, the character on-line, I want to give him a protection status, should do so.
DWORD dwrolestatus = Status_god;
At the same time, the character uses an item, the effect of this item when the HP and MP caps increase for some time. So add the status of the player's HP and MP caps, which should be added.
DWORD dwrolestatus |= (status_maxhp_adjust+status_maxmp_adjust);
Here is the |= instead of the = operation, because the previously attached invincible protection status cannot be cleared out. So use or operation.
This role is attacked by other players or monsters, and we want to determine if the protected state of the character being attacked is still in the state. Perform the following logic
if (Dwrolestatus & Status_god)//Determine if the bit is 1
{
Protected State, cannot be attacked
}
Next, the role of the invincible protection time expires, we want to clear the invincible state, do the following actions
Dwrolestatus &= ~status_god;
The inverse calculation is used here. The result of ~status_god is that the second bit is 0 and the others are 1. Then and Dwrolestatus do the bitwise and calculation.
status_god equals 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010;
~status_god equals 1111 1111 1111 1111 1111 1111 1111 1111 1111 1101;
Therefore, after the dwrolestatus phase, the Dwrolestatus, except for the second position, are preserved. The second bit, whatever the value, is set to 0, which clears the Status_god state. In the same vein, when we want to clear multiple states, we first need to clear the State or the operation together. Then take the inverse, then and dwrolestatus the bitwise with. To clear multiple states at the same time.
Then talk about XOR or it has a nature is that two times XOR that can restore back
e.g. a=7,b=6;
A = A^b^b
Let's take a look at that binary operation.
A = 0111
b = 0110
c = a^b = 0001
A = C^b = 0111
Write here, think of a classic C + + pen question, that is, no need for the 3rd variable, exchange the value of two variables.
A = A^b = 0001
b = B^a = 0111
A = A^b = 0110
Leetcode Reverse Bits