Tuesday, October 24, 2006

Use single value to store multiple settings

example use binary compare:

static final int problem1 = 0x1;
static final int problem2 = 0x10;
static final int problem3 = 0x100;

- when user selected problem1 and problem2
we can store the value like that

int userSelVal;
userSelVal = userSelVal | problem1;
userSelVal = userSelVal | problem2;

-then you will ask how to know the user is selected problem1 and problem2

if((userSelVal & problem1) == problem1){
//represent user select problem1
System.out.println("User is selected problem1");
}

if((userSelVal & problem2) == problem2){
//represent user select problem2
System.out.println("User is selected problem2");
}

if((userSelVal & problem3) == problem3){
//represent user select problem3
System.out.println("User is selected problem3");
}

- via to compare the binary position value, each position represent one setting then we can store int binary size setting in one int.