-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitDemo.java
More file actions
102 lines (83 loc) · 2.37 KB
/
BitDemo.java
File metadata and controls
102 lines (83 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.bit;
/**
* 位运算
*
*
*/
public class BitDemo {
/**
*
* &运算(与运算):两个二进制操作数对应位同为1 结果位 才为1,其余情况为0;
* &运算(与运算)中1&1=1,1&0=0,0&0=0
* 8 & 7 的运算步骤如下:
* 8的二进制是1000,7的二进制是0111
*
* 1000
* 0111
* 结果:
* 0000
*
* 把8的每一位都跟7的每一位进行运算,最高位 1&0=0, 后面的第二位是 0&1=0,依此类推。结果为 0000.
*
* 所以可以得出0000,故输出的是0。
*
*
* 1是0001,其他数跟 1做与运算,都只看最后一位即可,其他的都是0。
* 8&1,就是二进制1000和0001的与运算,结果得到 0000
* 7&1,就是二进制0111和0001的与运算,结果得到 0001。
* 某一个数x, x & 1 可以得到 x 除以 2 的余数。也就是说,可以用 x & 1 来判断奇偶数。
*
*/
public static void andDemo() {
int result = 8 & 7;
System.out.println(result);
}
/**
* |运算 (或运算):两个二进制操作数对应位只要有一个为1 结果位 就为1,其余情况为0;
* |运算 (或运算). 1|1=1,1|0=1,0|0=0
*
* 8 | 7 的运算步骤如下:
* 0111
* 1000
* 结果:
* 1111
*
* 1111转化为十进制是15,也就是说 8 | 7 = 15.
*/
public static void orDemo() {
int result = 8 | 7;
System.out.println(result);
}
/**
* 异或 a ^ b :两个二进制操作数对应位,相同为0,相异为1
*
*/
public static void xorDemo() {
int result = 5 ^ 10;
System.out.println(result);
}
/**
* ~ 取反 ~a:二进制操作数 0的为1,1的为0
*
*/
public static void reverseDemo() {
int result = ~5;
System.out.println(result);
}
/**
* 左移一位,相当于乘以2。
*
*/
public static void leftDemo() {
int result = 7 << 1;
System.out.println(result);
}
/**
* 右移一位,相当于除以2。
*
*/
public static void rightDemo() {
int result = 8 >> 1;
System.out.println(result);
}
}