Skip to content

Commit 931da82

Browse files
java位运算
1 parent e6b9259 commit 931da82

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

src/main/java/com/bit/BitDemo.java

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.bit;
2+
3+
/**
4+
* 位运算
5+
*
6+
*
7+
*/
8+
public class BitDemo {
9+
10+
/**
11+
*
12+
13+
* &运算(与运算)中1&1=1,1&0=0,0&0=0
14+
15+
* 8 & 7 的运算步骤如下:
16+
* 8的二进制是1000,7的二进制是0111
17+
*
18+
* 1000
19+
* 0111
20+
* 结果:
21+
* 0000
22+
*
23+
* 把8的每一位都跟7的每一位进行运算,最高位 1&0=0, 后面的第二位是 0&1=0,依此类推。结果为 0000.
24+
*
25+
* 所以可以得出0000,故输出的是0。
26+
*
27+
*/
28+
public static void andDemo(int num1, int num2) {
29+
int result = num1 & num2;
30+
System.out.println(result);
31+
32+
}
33+
34+
35+
36+
/**
37+
* |运算 (或运算). 1|1=1,1|0=1,0|0=0
38+
*
39+
* 8 | 7 的运算步骤如下:
40+
* 0111
41+
* 1000
42+
* 结果:
43+
* 1111
44+
*
45+
*/
46+
public static void orDemo(int num1, int num2) {
47+
int result = num1 | num2;
48+
System.out.println(result);
49+
50+
}
51+
52+
53+
/**
54+
* 异或 a ^ b 相同为0,相异为1
55+
*
56+
* @param num1
57+
* @param num2
58+
*/
59+
public static void xorDemo(int num1, int num2) {
60+
int result = num1 ^ num2;
61+
System.out.println(result);
62+
63+
}
64+
65+
66+
/**
67+
* ~ 取反 ~a 0的为1,1的为0
68+
*
69+
*/
70+
public static void reverseDemo(int num) {
71+
int result = ~num;
72+
System.out.println(result);
73+
}
74+
75+
76+
/**
77+
* 左移一位,相当于乘以2。
78+
*
79+
*/
80+
public static void leftDemo(int num) {
81+
int result = num << 1;
82+
System.out.println(result);
83+
}
84+
85+
86+
/**
87+
* 右移一位,相当于除以2。
88+
*
89+
*/
90+
public static void rightDemo(int num) {
91+
int result = num >> 1;
92+
System.out.println(result);
93+
}
94+
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package test.com.bit;
2+
3+
import com.bit.BitDemo;
4+
import org.junit.Test;
5+
import org.junit.Before;
6+
import org.junit.After;
7+
8+
/**
9+
* BitDemo Tester.
10+
*
11+
* @author <Authors name>
12+
* @since <pre>6, 6, 2022</pre>
13+
* @version 1.0
14+
*/
15+
public class BitDemoTest {
16+
17+
@Before
18+
public void before() throws Exception {
19+
}
20+
21+
@After
22+
public void after() throws Exception {
23+
}
24+
25+
/**
26+
*
27+
* Method: andDemo(int num1, int num2)
28+
*
29+
*/
30+
@Test
31+
public void testAndDemo() throws Exception {
32+
//TODO: Test goes here...
33+
BitDemo.andDemo(8, 7);
34+
}
35+
36+
/**
37+
*
38+
* Method: orDemo(int num1, int num2)
39+
*
40+
*/
41+
@Test
42+
public void testOrDemo() throws Exception {
43+
//TODO: Test goes here...
44+
BitDemo.orDemo(8, 7);
45+
}
46+
47+
/**
48+
*
49+
* Method: XorDemo(int num1, int num2)
50+
*
51+
*/
52+
@Test
53+
public void testXorDemo() throws Exception {
54+
//TODO: Test goes here...
55+
BitDemo.xorDemo(8, 7);
56+
}
57+
58+
/**
59+
*
60+
* Method: ReverseDemo(int num)
61+
*
62+
*/
63+
@Test
64+
public void testReverseDemo() throws Exception {
65+
//TODO: Test goes here...
66+
BitDemo.reverseDemo(5);
67+
}
68+
69+
/**
70+
*
71+
* Method: leftDemo(int num)
72+
*
73+
*/
74+
@Test
75+
public void testLeftDemo() throws Exception {
76+
//TODO: Test goes here...
77+
BitDemo.leftDemo(10);
78+
}
79+
80+
/**
81+
*
82+
* Method: rightDemo(int num)
83+
*
84+
*/
85+
@Test
86+
public void testRightDemo() throws Exception {
87+
//TODO: Test goes here...
88+
BitDemo.rightDemo(20);
89+
}
90+
91+
92+
}

0 commit comments

Comments
 (0)