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
| package org.yzh.protocol.commons;
|
| /**
| * 32位整型的二进制读写
| * << 左移
| * &与 同为1返回1
| * |或 其中一个为1返回1
| * ^异或 相同为0不同为1
| * @author yezhihao
| * https://gitee.com/yezhihao/jt808-server
| */
| public class Bit {
|
| /**
| * 判断n的第i位
| * @param n int32
| * @param i 取值范围0~31
| */
| public static boolean isTrue(int n, int i) {
| return get(n, i) > 0;
| }
|
| /**
| * 读取n的第i位
| * @param n int32
| * @param i 取值范围0~31
| */
| public static int get(int n, int i) {
| return (1 << i) & n;
| }
|
| /**
| * 设置n的第i位为1
| * @param n int32
| * @param i 取值范围0~31
| */
| public static int set1(int n, int i) {
| return (1 << i) | n;
| }
|
| /**
| * 设置n的第i位为0
| * @param n int32
| * @param i 取值范围0~31
| */
| public static int set0(int n, int i) {
| return get(n, i) ^ n;
| }
|
| /**
| * 写入bool到n的第i位
| * @param n int32
| * @param i 取值范围0~31
| */
| public static int set(int n, int i, boolean bool) {
| return bool ? set1(n, i) : set0(n, i);
| }
|
| /**
| * 按位写入int
| * 数组第一个int为首位
| */
| public static int writeInt(int... bit) {
| int n = 0;
| for (int i = 0; i < bit.length; i++)
| n = set(n, i, bit[i] > 0);
| return n;
| }
| }
|
|