Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a bitmask parameter for a function or method

Tags:

java

android

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?

like image 244
synic Avatar asked Apr 30 '10 19:04

synic


1 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
  }
}
like image 190
drawnonward Avatar answered Nov 10 '22 10:11

drawnonward