Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Android Bitflag

a guest May 2nd, 2013 402 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. public class Flag {
  2. //  final static int FLAG_A = 1 << 0;
  3. //  final static int FLAG_B = 1 << 1;
  4. //  final static int FLAG_C = 1 << 2;
  5. //  final static int FLAG_D = 1 << 3;
  6. //  final static int FLAG_E = 1 << 4;
  7. //  final static int FLAG_F = 1 << 5;
  8.     final static int FLAG_A = 0x01;
  9.     final static int FLAG_B = 0x02;
  10.     final static int FLAG_C = 0x04;
  11.     final static int FLAG_D = 0x08;
  12.     final static int FLAG_E = 0x10;
  13.     final static int FLAG_F = 0x20;
  14.    
  15.     static int flag = 0;
  16.    
  17.     public static void test() {
  18.         flag = FLAG_B|FLAG_C|FLAG_F;
  19.        
  20.         printFlag(); // flag : FLAG_B FLAG_C FLAG_F
  21.         checkA(); // I don't have FLAG_A
  22.        
  23.         // flag에 FLAG_A 추가
  24.         flag |= FLAG_A;
  25.        
  26.         printFlag(); // flag : FLAG_A FLAG_B FLAG_C FLAG_F
  27.         checkA(); // I have FLAG_A
  28.        
  29.         // flag에 FLAG_A 제거
  30.         flag &= ~FLAG_A;
  31.        
  32.         printFlag(); // flag : FLAG_B FLAG_C FLAG_F
  33.         checkA(); // I don't have FLAG_A
  34.     }
  35.    
  36.     public static void checkA() {
  37.         if ((flag&FLAG_A) != 0) {
  38.             Log.i("flag", "I have FLAG_A");
  39.         } else {
  40.             Log.i("flag", "I don't have FLAG_A");
  41.         }
  42.     }
  43.    
  44.     public static void printFlag() {
  45.         String result = "flag : ";
  46.         if ((flag&FLAG_A) != 0) result += "FLAG_A ";
  47.         if ((flag&FLAG_B) != 0) result += "FLAG_B ";
  48.         if ((flag&FLAG_C) != 0) result += "FLAG_C ";
  49.         if ((flag&FLAG_D) != 0) result += "FLAG_D ";
  50.         if ((flag&FLAG_E) != 0) result += "FLAG_E ";
  51.         if ((flag&FLAG_F) != 0) result += "FLAG_F ";
  52.         Log.i("flag", result);
  53.     }
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top