synic
synic

Reputation: 26678

Creating a bitmask parameter for a function or method

I noticed a lot of Android functions have a parameter that you can pass in that's a bitmask, for different options, like on PendingIntent, you can pass in things like you can call getActivity() with PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_NO_CREATE.

I'm wondering how I can create a function that has a parameter like this?

Upvotes: 6

Views: 3787

Answers (3)

Sumit Garai
Sumit Garai

Reputation: 1331

When we do a single << (left shift) to a int value,the binary representation of the int value gets a 0 from the right,and the left most bit is discarded,this has a effect of the original value of the int being doubled that means after each single left shift we will get double of the original value,this works well until the 31th indexed (starting indexing from 0 at right) left most value being set to 1 because of the use of << ,in that case instead of the value being double of the original it becomes a negative int value.

Now all the positive powers of 2 are 1,2,4,8,16,32 , ...... , 1073741824 for int values. These values contain only single '1' in their binary representation at some index.

This has a profit that when you | (bitwise or) any two of these powers of 2,the corresponding indices of '1' for both the values are set to '1' s in the resulting value.

for example 8 (1000) has 1 at the index 3,and 2 (10) has 1 at the index 1,when we | (or) them we get 1010 in binary that means both the 1 and 3 indexed bits are set to 1.

So if you want to check which flags are combined,you only need to check for the indices of the '1' s.

For example-

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;
    }

/* For example if the decision method return a array which contains {6,3,1,0,0,0} then that means the test6,test3 and test1 flags are or-ed ( decision(test6|test3|test1) ). */

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

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

They're done manually, simply by defining flags at powers of two. This file uses the left bit-shift operator, but that's not required:

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

Upvotes: 1

drawnonward
drawnonward

Reputation: 53689

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
  }
}

Upvotes: 11

Related Questions