如何简化将几个布尔对象表示为位序列?
How to ease representation of several boolean objects as a bit sequence?
例如考虑 select一周中的几天,我们可以 select 一天、几天或什么都不做。
我正在尝试按如下方式存储该数据:
public static final int NONE = 0b0000000;
public static final int MONDAY = 0b0000001;
public static final int TUESDAY = 0b0000010;
public static final int WEDNESDAY = 0b0000100;
public static final int THURSDAY = 0b0001000;
public static final int FRIDAY = 0b0010000;
public static final int SATURDAY = 0b0100000;
public static final int SUNDAY = 0b1000000;
其中0
是NONE,11
是星期一和星期二,1000011
是星期一、星期二和星期日;
有没有办法,我可以以更具可读性的方式编写相同的内容?
编辑:enums
的使用在 dalvik(android jvm)中有不错的性能惩罚,请不要建议它们。
这是一个带有 BitSet
的示例(尽管为了清楚起见,建议使用 EnumSet
)。
BitSet bits = new BitSet();
// the below is all redundant
// none
bits.set(0, false);
// monday
bits.set(1, false);
// tuesday
bits.set(2, false);
// wed
bits.set(3, false);
// thu
bits.set(4, false);
// fri
bits.set(5, false);
// sat
bits.set(6, false);
// sun
bits.set(7, false);
// EOF redundant part
// mon + tue (last index exclusive)
bits.set(1, 3);
// comparison, tue + wed
BitSet compare = new BitSet();
// tue + wed
compare.set(2, 4);
System.out.println(bits.equals(compare)); // false
System.out.println(bits.intersects(compare)); // true
备注
枚举在 Android 中是 "discouraged" 用于内存消耗,但这并不是一个禁忌,具体取决于您有多少(另请参见 here)。
或者您可以使用 Calendar
常量作为索引。
例如:
bits.set(Calendar.MONDAY)
我认为这与您正在尝试做的类似。
public static final int NONE = 0;
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 4;
public static final int THURSDAY = 8;
public static final int FRIDAY = 16;
public static final int SATURDAY = 32;
public static final int SUNDAY = 64;
public static void main(String[] args) {
int z = Integer.MAX_VALUE ^ SUNDAY ^ MONDAY;
for (int x = 1; x <= 64; x = x * 2) {
System.out.println(z & x);
}
}
输出
0
2
4
8
16
32
0
为了喜欢代码优雅的 Whosebug 用户的利益,我将 post 一个使用 enum
s 的版本。
public enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday;
/**
* Demonstrates the simplicity.
*/
public String shortForm() {
return name().substring(0, 3);
}
}
// Elegant EnumSet initialisation.
private static final EnumSet weekEndDays = EnumSet.of(Day.Saturday, Day.Sunday);
public static final Set<Day> weekend = Collections.<Day>unmodifiableSet(weekEndDays);
public static final Set<Day> weekdays = Collections.<Day>unmodifiableSet(EnumSet.complementOf(weekEndDays));
public void test() {
for (Day day : weekdays) {
System.out.println(day.toString() + " or " + day.shortForm());
}
}
例如考虑 select一周中的几天,我们可以 select 一天、几天或什么都不做。
我正在尝试按如下方式存储该数据:
public static final int NONE = 0b0000000;
public static final int MONDAY = 0b0000001;
public static final int TUESDAY = 0b0000010;
public static final int WEDNESDAY = 0b0000100;
public static final int THURSDAY = 0b0001000;
public static final int FRIDAY = 0b0010000;
public static final int SATURDAY = 0b0100000;
public static final int SUNDAY = 0b1000000;
其中0
是NONE,11
是星期一和星期二,1000011
是星期一、星期二和星期日;
有没有办法,我可以以更具可读性的方式编写相同的内容?
编辑:enums
的使用在 dalvik(android jvm)中有不错的性能惩罚,请不要建议它们。
这是一个带有 BitSet
的示例(尽管为了清楚起见,建议使用 EnumSet
)。
BitSet bits = new BitSet();
// the below is all redundant
// none
bits.set(0, false);
// monday
bits.set(1, false);
// tuesday
bits.set(2, false);
// wed
bits.set(3, false);
// thu
bits.set(4, false);
// fri
bits.set(5, false);
// sat
bits.set(6, false);
// sun
bits.set(7, false);
// EOF redundant part
// mon + tue (last index exclusive)
bits.set(1, 3);
// comparison, tue + wed
BitSet compare = new BitSet();
// tue + wed
compare.set(2, 4);
System.out.println(bits.equals(compare)); // false
System.out.println(bits.intersects(compare)); // true
备注
枚举在 Android 中是 "discouraged" 用于内存消耗,但这并不是一个禁忌,具体取决于您有多少(另请参见 here)。
或者您可以使用 Calendar
常量作为索引。
例如:
bits.set(Calendar.MONDAY)
我认为这与您正在尝试做的类似。
public static final int NONE = 0;
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 4;
public static final int THURSDAY = 8;
public static final int FRIDAY = 16;
public static final int SATURDAY = 32;
public static final int SUNDAY = 64;
public static void main(String[] args) {
int z = Integer.MAX_VALUE ^ SUNDAY ^ MONDAY;
for (int x = 1; x <= 64; x = x * 2) {
System.out.println(z & x);
}
}
输出
0
2
4
8
16
32
0
为了喜欢代码优雅的 Whosebug 用户的利益,我将 post 一个使用 enum
s 的版本。
public enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday;
/**
* Demonstrates the simplicity.
*/
public String shortForm() {
return name().substring(0, 3);
}
}
// Elegant EnumSet initialisation.
private static final EnumSet weekEndDays = EnumSet.of(Day.Saturday, Day.Sunday);
public static final Set<Day> weekend = Collections.<Day>unmodifiableSet(weekEndDays);
public static final Set<Day> weekdays = Collections.<Day>unmodifiableSet(EnumSet.complementOf(weekEndDays));
public void test() {
for (Day day : weekdays) {
System.out.println(day.toString() + " or " + day.shortForm());
}
}