bitmask question?

后端 未结 6 1909
面向向阳花
面向向阳花 2021-01-31 04:46

I have the follow:

public static final int LIMIT_ONE = 1;
public static final int TRADEABLE = (1 << 1);
public static final int SELLABLE = (1 << 2);
         


        
相关标签:
6条回答
  • 2021-01-31 05:03

    My guess would be that you take some number, such as your example 12414, and figure out what properties are contained within it.

    For example, since 12414 is 11000001111110 in binary, whatever it is attached to is tradeable, because ANDing this number with the mask will give you a 1 in the second bit.

    0 讨论(0)
  • 2021-01-31 05:03

    In binary, 12414 is 11000001111110. LIMIT_ONE in binary is 1 and the <<, which is the bitshift operator moves the zero to the left padding with a zero on the right. Therefore, tradeable in binary is 10 and so on until unk16, which ends up being 1000000000000000. Now you put these values together using bitwise OR, which basically puts a 1 on each position where at least one of its operand has a one on that position (the pipe operator '|' is used in most languages).

    Example:

    100 | 10 = 110
    

    Therefore, to get to 12414, you need to do a bitwise OR on the following variables: unk16, unk15, tradeable, selleable, storeable, storeable in wh, storeable in legion wh and breakable. The combination of ones on the different positions in each of these variables gives you binary 11000001111110, which turns out to be 12414 in decimal.

    This is probably the simplest way to explain it, if you want to know more, you should read up on bitwise operators and how binary representation of numbers works.

    To find out which of the flags the number 12414 has, you can use the & (bitwise AND) operator and do a zero check. For example:

    6 & 2 = 2 (110 has a 1 on the same position as 2, which is 010)
    6 & 1 = 0 (110 does not have a 1 on the same position as 1, which is 001)
    
    0 讨论(0)
  • 2021-01-31 05:08

    a << b shifts the bits in a b values to the left, padding the new bits on the right with zeroes. 1 << n equates to an integer with only the nth bit (counting from 0 from the right) set, which is equivalent to 2n.

    12414 is 11000001111110 in binary. Therefore it is produced by summing the constants listed below. You can work this out by seeing the bit 1 from the right is set, therefore TRADEABLE is "set"; bit 7 is not set (it's 0), therefore SOUL_BOUND is not "set". Note how the bit numbers associate with the declared values of (1 << n).

    TRADEABLE
    SELLABLE
    STORABLE
    STORABLE_IN_WH
    STORABLE_IN_LEGION_WH
    BREAKABLE
    BLACK_CLOUD_TRADERS
    CAN_SPLIT
    
    0 讨论(0)
  • 2021-01-31 05:15

    I don't understand the question "how it is calculated to give the follow result". (What is calculated?)

    The main thing to understand is that all computer values are stored in binary. Any number will be a combination of 0 and 1 bit. Some numbers only have one 1 bit.

    http://en.wikipedia.org/wiki/Mask_(computing)

    0 讨论(0)
  • 2021-01-31 05:16

    The expression (1 << n) is equivalent to 2 raised to the power of n.

    When you write (1 << n) | (1 << m) this is the same as (1 << n) + (1 << m) as long as n and m are different. So you can think of it in terms of simple additions if you wish.

    The number 12414 in binary is 11000001111110 so it is the sum (or bitwise OR) of the following flags:

    TRADEABLE                1 << 1  =     2
    SELLABLE                 1 << 2  =     4
    STORABLE                 1 << 3  =     8
    STORABLE_IN_WH           1 << 4  =    16
    STORABLE_IN_LEGION_WH    1 << 5  =    32
    BREAKABLE                1 << 6  =    64
    BLACK_CLOUD_TRADERS      1 << 12 =  4096
    CAN_SPLIT                1 << 13 =  8192
    ========================================
                             Total   = 12414
    

    Note that the flags that are included correspond to the bits that are set in the binary representation of 12414, when read right-to-left.

    0 讨论(0)
  • 2021-01-31 05:21

    12414 in binary is:

    Binary number: 1  1  0  0  0  0  0  1  1  1  1  1  1  0
    -------------------------------------------------------
    Bit positions: 13 12 11 10 9  8  7  6  5  4  3  2  1  0
    

    Look at which bits are 1. Those are the flags that are set in the bitmask, which is created by using the bitwise OR operator to combine the flags:

    bitmask = TRADEABLE | SELLABLE | STORABLE | STORABLE_IN_WH | STORABLE_IN_LEGION_WH | BREAKABLE | BLACK_CLOUD_TRADERS | CAN_SPLIT;
    

    To further explain this, STORABLE = (1 << 3); means that STORABLE is equal to the number one (binary 1, falling only in bit position 0) shifted to the left by 3 places. Note that STORABLE = Math.pow(2, 3); would be equivalent. Because none of the bits overlap among the flags, we can combine all of them into a single int and then split them apart later.

    We can check for the existence of flags using the bitwise AND operator, which will return a non-zero value if the flag is set and a zero value if the flag is not set:

    if(bitmask & TRADEABLE != 0) {
        // This item can be traded
    } else {
        // This item cannot be traded
    }
    

    We can set, clear, or toggle flags like this:

    bitmask |= TRADEABLE; // Sets the flag using bitwise OR
    bitmask &= ~TRADEABLE; // Clears the flag using bitwise AND and NOT
    bitmask ^= TRADEABLE; // Toggles the flag using bitwise XOR 
    
    0 讨论(0)
提交回复
热议问题