lizhanwei
2020-02-18 87ab0bd072bd42c09a649759090942781ab53fcb
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
package com.safeluck.aykj.utils;
 
 
public class BitState {
    public BitState(int b)
    {
        this.value = b;
        this.total = 32;
    }
    public BitState(byte b)
    {
        this.value = b;
        this.total = 8;
    }
    public BitState(short b)
    {
        this.value = b;
        this.total = 16;
    }
 
    public int total = 32;
 
    public int value;
 
    public boolean get(int bit)
    {
        int pos = bit;//32 - bit;
        return (this.value >> pos & 1) == 1;
    }
    public void set(int pos, boolean state)
    {
        //11000110
        int move_pos = pos;// 32 - pos;
        if (state)
        {
            this.value = this.value | 1 << move_pos;
        }
        else
        {
            this.value = this.value & ~(1 << move_pos);
        }
    }
 
    @Override
    public String toString()
    {
        String str = Integer.toBinaryString(this.value);
        return getPaddingRightString(str, this.total,"00");
    }
    String getPaddingRightString(String str, int total_len,String padding) {
        while (str.length() < total_len) {
            str = str+padding;
        }
        return str;
    }
}