10进制转2进制
2048 1024 512 256 128 64 32 16 8 4 2 1
与运算 表示2进制位上 表示都是1才是1,可以理解为必须是男同才是真爱,女同不算
window计算器符号标记为AND
或运算 OR
01(8) 0 1 0 // =2+8 =10
0 1 1 // = 10 和3 |运算
===================
1 0 1 1
//与运算 结果 10|3结果为 3|8 =11 WINDOW计算器验证 3 OR 8 =11
与运算 AND
01(8) 0 1 0 // =2+8 =10
0 1 1 // = 10 和3&运算
===================
0 0 1 0
//与运算 结果 10&8 =0 WINDOW计算器验证 3 AND 10 =2
异或运算 XOR
XO可以理解为xxoo,必须男生和女生,为同性则不行.
0 1 0 //2
0 0 1 // 1
====
2^1=3 //前面2位成立
取反运算 Not
取反的意思就是1,变成0 ,这里不和其他数参与运算
1 取反后的结果是-2
0 1 //自身取反之后是 100 也就是 1111 1111 1111 1111 1111 1111 1111 1110
//16进制是 fffffffe
//10进制是-2
取模 MOD
必须大的放到前面
假设日志每5次打印一次。那么0%5==0以及5%5==0 以及10%5==0
6%5 =1
右移运算 RSH
1往右边1一位是0 无负数符号位移
2右边移动1位就是1,这个没啥可说的就是二进制的移位置而已,不头晕,就是负数 也就是有符号的移位有点绕,曾经明白了,现在就又糊涂了。
-1 的二进制位全是1,右移动一位依然是-1 这符号位继承了
左移运算 LSH
1左边移动一位就是2,这也没啥可说的。
来看看-1左移动一位 移动之后补0, 为什么计算器结果得到的是-2呢?
原理如下:
强制转换是用补码运算 在 按位与也是用补码的方式,但是正数 补码原码一样
原码-反码---补码
补码做运算结果也是补码
电脑内存存的是补码
无符号左右移 负数先从原码-反码-补码-然后补码(也就是对补码进行取反)的符号改为0
有符号左右移 负数先从原码-反码-补码-然后补码的符号改为照填,因为计算机运算是补码运算
左移 左边最高位丢弃,右边移空的补0,
右移 左边移空的左边最高为是1就补1,是0就补0,移动几位补几位,结果肯定是正数
右移负数 所以左边的是补1.
########### 什么是反码
正数的反码是其本身
负数的反码是在其原码的基础上, 符号位不变,其余各个位取反.
########### 什么是补码
正数的补码就是其本身
负数的补码是在其原码的基础上, 符号位不变, 其余各位取反, 最后+1. (即在反码的基础上+1)
[+1] = [00000001]原 = [00000001]反 = [00000001]补
[-1] = [10000001]原 = [11111110]反 = [11111111]补
=================这里操作 左边移动1位,
1 1 1 1 1 1 1 0 原码
1 0 0 0 0 0 0 1反码[-1] 符号位不变,其它全部取反
1 1 1 1 1 1 1 0补码[再次取反 但是还需要+1] 这里表示-2,但是人类是看不太懂的。
-2 取反=1 的原因也知道了吧,因为 -2 1 1 1 1 1 1 1 0 如果取反之后那么前面全是0 最后面一个是 1.
android的与或运算
short reuslt=0b0011&~0b1100;//为毛是0x11也就是3,这里就是删除右边的值得意思
System.out.println(Integer.toBinaryString(reuslt));
reuslt=0b0011&0b1100; //没有一个是相等所以是0
System.out.println(Integer.toBinaryString(reuslt));
short value=~0b1;
System.out.println(Integer.toBinaryString(value));
System.out.println(value);
结果
11
0
11111111111111111111111111111110
-2
android的写法
,而且那个值也不会是正数,所以删除的一般是最高位,
而测量规格measureSpec通常是mode+size 所以才有了这样的写法
measureSpec&~MODE_MASK =size
//删除模式标记得到真实的尺寸,通常测量规格里面还携带了模式,也得到真实的宽度得这样移除标记
measureSpec&MODE_MASK=mode
//与运算为1才是1, 而android源码中给的mask通常后面不是1, 因为这里前面要存尺寸大小的,可不能混在一起,混在一起就乱了,那么系统是如何写的呢?
int MODE_SHIFT = 30;
int MODE_MASK = 0x3 << MODE_SHIFT;
// 0x3 左移动30位=01 00 0000 0000 0000 0000 000
int UNSPECIFIED = 0 << MODE_SHIFT;
int EXACTLY = 1 << MODE_SHIFT;
int AT_MOST = 2 << MODE_SHIFT;
System.out.println("UNSPECIFIED OX:"+Integer.toBinaryString(UNSPECIFIED));
System.out.println("M OX:"+Integer.toBinaryString(MODE_MASK));
System.out.println("E OX:"+Integer.toBinaryString(EXACTLY));//MATCH_PARETN或者精确值
System.out.println("A OX:"+Integer.toBinaryString(AT_MOST));//WRAP_CONTENT
UNSPECIFIED OX:0// UNSPECIFIED
M OX:11000000000000000000000000000000//
// MODE_MASK 清除筛选标记可以看出来他们各在同一个标记位互不冲突而且后面全是0,因为避免和值冲突,当然最后倒数第二位这么宽的宽度基本上不可能的,所以用2 个 最高位 用三个最高位都无妨,无妨。
E OX:01000000000000000000000000000000//EXACTLY MATCH_PARETN
A OX:10000000000000000000000000000000// AT_MOST WRAP_CONTENT
下面这些也很有意思,你会发现2 1 ,4 100 200 但是为什么很少有3有5, 也应该懂了吧,比如设置显示隐藏的, 为毛它不弄个1 2 3的,基本是是2的倍数。 另外触摸事件的3,所以我发现很少用来做&运算。
public static final int VISIBLE = 0x00000000;
public static final int INVISIBLE = 0x00000004;
public static final int GONE = 0x00000008;
public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
public static final int SYSTEM_UI_FLAG_IMMERSIVE = 0x00000800;
public static final int SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 0x00001000;
public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
blic static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
public static final int STATUS_BAR_DISABLE_SEARCH = 0x02000000;
public static final int NAVIGATION_BAR_TRANSIENT = 0x08000000;
public static final int STATUS_BAR_UNHIDE = 0x10000000;
public static final int STATUS_BAR_TRANSLUCENT = 0x40000000;
public static final int NAVIGATION_BAR_TRANSLUCENT = 0x80000000;
public static final int RECENT_APPS_VISIBLE = 0x00004000;
public static final int SYSTEM_UI_TRANSPARENT = 0x00008000;
static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x00003FFF;
public static final int SYSTEM_UI_CLEARABLE_FLAGS =
SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
| SYSTEM_UI_FLAG_FULLSCREEN;
public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
public static final int KEEP_SCREEN_ON = 0x04000000;
public static final int VISIBLE = 0x00000000;
public static final int INVISIBLE = 0x00000004;
public static final int GONE = 0x00000008;
static final int VISIBILITY_MASK = 0x0000000C;
private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
static final int ENABLED = 0x00000000;
static final int DISABLED = 0x00000020;
static final int ENABLED_MASK = 0x00000020;
static final int WILL_NOT_DRAW = 0x00000080;
static final int DRAW_MASK = 0x00000080;
static final int SCROLLBARS_NONE = 0x00000000;
static final int SCROLLBARS_HORIZONTAL = 0x00000100;
static final int SCROLLBARS_VERTICAL = 0x00000200;
static final int SCROLLBARS_MASK = 0x00000300;
static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
static final int FADING_EDGE_NONE = 0x00000000;
static final int FADING_EDGE_HORIZONTAL = 0x00001000;
static final int FADING_EDGE_VERTICAL = 0x00002000;
static final int FADING_EDGE_MASK = 0x00003000;
static final int CLICKABLE = 0x00004000;
static final int DRAWING_CACHE_ENABLED = 0x00008000;
static final int SAVE_DISABLED = 0x000010000;
static final int SAVE_DISABLED_MASK = 0x000010000;
static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
};
static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
static final int LONG_CLICKABLE = 0x00200000;
static final int DUPLICATE_PARENT_STATE = 0x00400000;
static final int CONTEXT_CLICKABLE = 0x00800000;
public static final int INVALID_POINTER_ID = -1;
public static final int ACTION_MASK = 0xff;
public static final int ACTION_DOWN = 0;
public static final int ACTION_UP = 1;
public static final int ACTION_MOVE = 2;
public static final int ACTION_CANCEL = 3;
public static final int ACTION_OUTSIDE = 4;
public static final int ACTION_POINTER_DOWN = 5;
public static final int ACTION_POINTER_UP = 6;
public static final int ACTION_HOVER_MOVE = 7;
public static final int ACTION_SCROLL = 8;
public static final int ACTION_HOVER_ENTER = 9;
public static final int ACTION_HOVER_EXIT = 10;
public static final int ACTION_BUTTON_PRESS = 11;
public static final int ACTION_BUTTON_RELEASE = 12;
public static final int ACTION_POINTER_INDEX_MASK = 0xff00;
public static final int ACTION_POINTER_INDEX_SHIFT = 8;
public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000;
public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100;
public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200;
public static final int ACTION_POINTER_1_UP = ACTION_POINTER_UP | 0x0000;
public static final int ACTION_POINTER_2_UP = ACTION_POINTER_UP | 0x0100;
public static final int ACTION_POINTER_3_UP = ACTION_POINTER_UP | 0x0200;
public static final int ACTION_POINTER_ID_MASK = 0xff00;
public static final int ACTION_POINTER_ID_SHIFT = 8;
写的比较好的一篇反码补码文章
https://www.jianshu.com/p/3d92fe1c34af?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation