When should I use this kind of int variables?

后端 未结 5 1050
忘了有多久
忘了有多久 2021-01-15 23:48

Some int variables in java.awt.Event class is set by bitwise. Like below:

/**
 * This flag indicates that the Shift key was down when the ev         


        
5条回答
  •  日久生厌
    2021-01-16 00:34

    Why do this? As pointed out in the other answers, the values are powers of two and you are thereby setting a bit. Suppose you wanted to test for a combination of conditions.

    You could then combine bits like for example,

    SOME_COMBO_CONDITION = SHIFT_MASK | ALT_MASK

    and then test for that condition by testing if both bits are set by doing

    current_condition & SOME_COMBO_CONDITION

    Using integer codes you'd have to test using a more complex conditional.

提交回复
热议问题