Creating a bitmask parameter for a function or method

I noticed that many Android functions have a parameter that you can pass in a bitmask, for different parameters, for example, in PendingIntent, you can pass things like you can call getActivity () with PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_NO_CREATE.

I am wondering how can I create a function with this parameter?

+5
source share
3 answers
public static final int FLAG_1 = 1<<0; // 0x01
public static final int FLAG_2 = 1<<1; // 0x02
public static final int FLAG_3 = 1<<2; // 0x04
public static final int FLAG_4 = 1<<3; // 0x08

public void myFlagsFunction( int flags ) {
  if ( 0 != ( flags & FLAG_1 ) ) {
    // do stuff
  }
  if ( 0 != ( flags & FLAG_2 ) ) {
    // do stuff
  }
}
+10
source

They are executed manually, simply by defining flags with two permissions. This file uses the left bit changer, but this is not required:

public static final int FLAG_ONE_SHOT = 1<<30;
//...
public static final int FLAG_NO_CREATE = 1<<29;
+1
source

< ( ) int, int 0 , , int, , , , 31- ( 0 ) 1 - <, , , int.

2 1,2,4,8,16,32,......, 1073741824 int. '1' .

, | ( ) 2, "1" "1" .

, 8 (1000) 1 3, 2 (10) 1 1, | () 1010 , , 1 3 1.

, , , "1" .

-

static int test0= 1;
static int test1= 2;
static int test2= 4;
static int test3= 8;
static int test4= 16;
static int test5= 32;
static int test6= 64;
int[] result;
int[] decision(int flags){
    char[] x=Integer.toBinaryString(flags).toCharArray();
    result=new int[x.length];
    for(int i=x.length-1,j=0,k=0;i>=0;i--)
        switch (x[k++]){
            case '0':
            break;
            case '1':
            if(i==0){
                result[j++]=-1; //for ...000001 (test0) put -1 into the array instead of the value of i which is 0.
                break;
            }
            result[j++]=i;
            };
        return result;
    }

/* , , {6,3,1,0,0,0}, , test6, test3 test1 or-ed ( (test6 | test3 | test1)). */

Thanks to the above two answers, I had a question about how the methods you were talking about work, I got an answer from the two above answers. The actual implementation works as shown in the previous two answers.

0
source

All Articles