2013-07-27 42 views

回答

8

按位或分配。

int a = 6, b = 5; 
a = a | b; 
// is equivalent to 
a |= b; 

在像Android這樣的系統中,將許多不同的布爾屬性壓縮爲一個整數值通常是有意義的。數值可以與|相結合,並使用&進行測試。

// invented constants for example 
public static final int HAS_BORDER = 1; // in binary: 0b00000001 
public static final int HAS_FRAME = 2; //   0b00000010 
public static final int HAS_TITLE = 4; //   0b00000100 

public void exampleMethod() { 
    int flags = 0;       // flags = 0b00000000 
    flags |= HAS_BORDER;     //   0b00000001 
    flags |= HAS_TITLE;      //   0b00000101 

    if ((flags & HAS_BORDER) != 0) { 
    // do x 
    } 

    if ((flags & HAS_TITLE) != 0) { 
    // do y 
    } 
} 
+1

'a = a | b'相當於'a = a | B'? –

+0

@LuiggiMendoza謝謝。這很愚蠢。 –

4

同:

noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL; 

所有位OR「編在一起。這通常是加上一個標誌到一組現有標誌的好方法,因爲Notification.FLAG_AUTO_CANCEL可能只是設置了一個位,所以該位將是開啓noti.flags