二进制运算
-
与运算(&):同1为1 例如:0&0=0;0&1=0;1&0=0;1&1=1 -
或运算(|):有1为1 例如:0|0=0; 0|1=1;1|0=1;1|1=1; -
异或运算(^):不同为1 例如:0^0=0; 01=1;10=1;1^1=0; -
取反(~):二进制中1变0;0变1 例如:二进制1010;取反后就是0101 -
Integer.toBinaryString(int):将整型值转换成二进制字符串
基本原理
int最大值是2的31次方减1=2147483647,用二进制表示就是32个1,每一位上都可以用1和0来表示开关的状态;所以一个int值最多可以存储32个开关的状态,可以达到节约内存和提高性能的目的
代码示例
package com.example.myapplication.binaryint;
class BinaryIntTest {
private static final int FLAG_IN_USE = 1 << 0;
private static final int FLAG_OTHER = 1 << 1;
private static final int FLAG_OTHER1 = 1 << 2;
private static final int FLAG_OTHER2 = 1 << 3;
private static final int FLAG_OTHER3 = 1 << 4;
private int flags;
public void setFlagValue(boolean isUse, int flag) {
if (isUse) {
flags |= flag;
} else {
flags &= ~flag;
}
System.out.println(Integer.toBinaryString(flags));
}
public static void main(String[] args) {
BinaryIntTest test = new BinaryIntTest();
System.out.println(Integer.toBinaryString(test.flags));
test.setFlagValue(true, FLAG_IN_USE);
test.setFlagValue(true, FLAG_OTHER);
test.setFlagValue(true, FLAG_OTHER1);
test.setFlagValue(true, FLAG_OTHER2);
test.setFlagValue(true, FLAG_OTHER3);
test.setFlagValue(false, FLAG_OTHER2);
}
}
运行结果:
0
1
11
111
1111
11111
10111
Process finished with exit code 0
Android应用
Android中很多地方都用到了这种方式,比如MeasureSpec、Message等;
模拟MeasureSpec
MeasureSpec是前2位用来记录mode,后30位记录size
package com.example.myapplication.binaryint;
import java.security.PublicKey;
class BinaryIntTest {
static class MeasureSpec {
private int measureSpec;
private static final int MODE_SHIFT = 3 << 30;
static final int UNSPECIFIED = 0 << 30;
static final int EXACTLY = 1 << 30;
static final int AT_MOST = 2 << 30;
public int getMode() {
return measureSpec & MODE_SHIFT;
}
public int getSize() {
return measureSpec & ~MODE_SHIFT;
}
public int makeMeasureSpec(int mode, int size) {
measureSpec = 0;
measureSpec |= mode;
measureSpec |= size;
return measureSpec;
}
public void setMode(int mode) {
int size = getSize();
measureSpec = 0;
makeMeasureSpec(mode, size);
}
public void setSize(int size) {
int mode = getMode();
measureSpec = 0;
makeMeasureSpec(mode, size);
}
public String getModeName() {
String modeName = "未知";
switch (getMode()) {
case MeasureSpec.AT_MOST:
modeName = "AT_MOST";
break;
case MeasureSpec.EXACTLY:
modeName = "EXACTLY";
break;
case MeasureSpec.UNSPECIFIED:
modeName = "UNSPECIFIED";
break;
}
return modeName;
}
}
public static void main(String[] args) {
MeasureSpec measureSpec = new MeasureSpec();
measureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 1080);
System.out.println(Integer.toBinaryString(measureSpec.getMode()) + "=>" + measureSpec.getModeName());
System.out.println(Integer.toBinaryString(measureSpec.getSize()) + "=>" + measureSpec.getSize());
System.out.println("修改后=》》》》》》》");
measureSpec.setMode(MeasureSpec.AT_MOST);
measureSpec.setSize(1920);
System.out.println(Integer.toBinaryString(measureSpec.getMode()) + "=>" + measureSpec.getModeName());
System.out.println(Integer.toBinaryString(measureSpec.getSize()) + "=>" + measureSpec.getSize());
}
}
运行结果:
0=>UNSPECIFIED
10000111000=>1080
修改后=》》》》》》》
10000000000000000000000000000000=>AT_MOST
11110000000=>1920
Process finished with exit code 0
|