Question : Java Swing logical operator

I have some codes extracted from DefaultButtonModel.java. I am wondering what does following operation mean:

            stateMask |= ENABLED;
            stateMask &= ~PRESSED;

stateMask is int, ENABLED is final int (1 << 3), PRESSED is int(1<<2).

 public final static int PRESSED = 1 << 2;
 public final static int ENABLED = 1 << 3;

Does stateMask |= ENABLED mean: 1) stateMask OR ENABLED, then assign it back to stateMask?
Does stateMask &= ~PRESSED mean: 1) neg PRESS  first (0x0 to 0x1, 0x1 to 0x0), then AND with stateMask, result is assigned to stateMask?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
public void setEnabled(boolean b) {
        if(isEnabled() == b) {
            return;
        }
            
        if (b) {
            stateMask |= ENABLED;
        } else {
            stateMask &= ~ENABLED;
	    // unarm and unpress, just in case
            stateMask &= ~ARMED;
            stateMask &= ~PRESSED;
        }

            
        fireStateChanged();
    }
        
    /**
     * {@inheritDoc}
     */
    public void setSelected(boolean b) {
        if (this.isSelected() == b) {
            return;
        }

        if (b) {
            stateMask |= SELECTED;
        } else {
            stateMask &= ~SELECTED;
        }

        fireItemStateChanged(
                new ItemEvent(this,
                              ItemEvent.ITEM_STATE_CHANGED,
                              this,
                              b ?  ItemEvent.SELECTED : ItemEvent.DESELECTED));
        
        fireStateChanged();
        
    }

Answer : Java Swing logical operator

Hi learningunix,

the line checks if the left-most byte of 'num' is '1'.

'&num' is a pointer to the memory address where the first byte of 'num' resides. The '(char*)' casts this pointer '&num' (which is a pointer to int) to a pointer to char. Since char is a one byte data type accessing that 'pointer to char' with '*' accesses the first byte of the int. In little endian this byte has to be '1' for and 'int' which is '1' - in big endian the first byte would be '0' since the least significant byte is the most right one ...

Hope that helps,

ZOPPO

Random Solutions  
 
programming4us programming4us