异或

来源:互联网 发布:数据统计测试用例 编辑:程序博客网 时间:2024/04/29 16:47

在进行位运算时要将变量定义成int方便使用,用0x表示16进制,2位16进制位表示一个字节8位2进制的值清晰明了,而10进制不好表示2进制

public class ActiveInfo {    static public int SLOT_2_ADDRESS = 0x0010;    static public int SLOT_3_ADDRESS = 0x0018;    static public int SLOT_4_ADDRESS = 0x0020;    static public int SLOT_5_ADDRESS = 0x0028;    static public int SLOT_6_ADDRESS = 0x0030;    static public int SLOT_7_ADDRESS = 0x0038;    static public int SLOT_8_ADDRESS = 0x0040;    static public int SLOT_9_ADDRESS = 0x0048;    static public int SLOT_10_ADDRESS = 0x0050;    static public int SLOT_11_ADDRESS = 0x0058;    static public int SLOT_12_ADDRESS = 0x0060;    static public int SLOT_13_ADDRESS = 0x0068;    static public int SLOT_14_ADDRESS = 0x0070;    static public int SLOT_15_ADDRESS = 0x0078;//    public static final byte ACTIVE_STATE_NOT = 0;//未激活状态(默认)    public static final int ACTIVE_STATE_FOREVER = 0x0010;//永久激活状态    public static final int ACTIVE_STATE_TIME = 0x0011;//次数激活状态    public static final int ACTIVE_TIMES_DEFAULT = 0x0032;//初始化激活次数    public static final int ACTIVE_CHECK = 0x00fa;//校验位    public static final int ACTIVE_RESULT_SUCCESS = 0x0000;//写入成功返回    static final int lengh = 3;//字节数组,第一位状态,第二位次数,第三位校验位    private int times;//剩余激活次数    private int state;//状态    public boolean readActiveInfo() {        if (EncryptChip.hasEncryptChip(0)) {            if (EncryptChip.getEncryptChipUid(0) != null && !EncryptChip.getEncryptChipUid(0).equals("retry")) {                long timeout = 1000;                while (timeout > 0) {                    try {                        byte[] bytes = EncryptChip.readFromChip(SLOT_2_ADDRESS, lengh, 0);                        bytes[0] = (byte) ((bytes[0]&0xff)^ACTIVE_CHECK);                        bytes[1] = (byte) ((bytes[1]&0xff)^ACTIVE_CHECK);                        if((bytes[2]&0xff)!=((bytes[0]&0xff)^(bytes[1]&0xff)^ACTIVE_CHECK)){                            L.i("readActiveInfo:校验失败");                            return false;                        }                        if (new String(bytes).equals("retry")) {                            L.i("readActiveInfo:读取失败");                        } else {                            L.i("readActiveInfo:读取成功");                            L.i("readActiveInfo_bytes[0]:"+bytes[0]);                            L.i("readActiveInfo_bytes[1]:"+bytes[1]);                            if ((bytes[0]&0xff) == ACTIVE_STATE_FOREVER) {                                state = ACTIVE_STATE_FOREVER;                            } else if ((bytes[0]&0xff) == ACTIVE_STATE_TIME) {                                times = bytes[1]&0xff;                                L.i("readActiveInfo:"+times);                                state = ACTIVE_STATE_TIME;                            }                            return true;                        }                        Thread.sleep(100);                        timeout -= 100;                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }        return false;    }    public int getState() {        return state;    }    public void setState(int state) {        this.state = state;    }    public int getTimes() {        return times;    }    public void setTimes(int times) {        this.times = times;    }    public boolean writeActiveInfo() {        byte[] data = new byte[lengh];        data[0] = (byte) (state^ACTIVE_CHECK);        data[1] = (byte) (times^ACTIVE_CHECK);        data[2] = (byte) (state^times^ACTIVE_CHECK);        long timeout = 1000;        while (timeout > 0) {            try {                int ret = -1;                synchronized (ActiveInfo.class) {                    ret = EncryptChip.writeIntoChip(SLOT_2_ADDRESS, data, 0);                }                if (ret == ACTIVE_RESULT_SUCCESS) {                    L.i("writeActiveInfo:写入成功");                    return true;                } else {                    L.i("writeActiveInfo:写入失败");                }                Thread.sleep(100);                timeout -= 100;            } catch (InterruptedException e) {                e.printStackTrace();            }        }        return false;    }}



数据校验的原理

(2011-03-07 17:03:14)
转载
标签:

杂谈

分类:学习经验

问题:在各种总线通信,或其他数据信息传递中为保证数据传递正确可靠,在数据帧中常加载异或校验位。

如下图中最简单的一个数据帧中所示:

 数据校验的原理

图中,校验位为8位数据,其是对其前面的命令字和数据进行异或校验。

我们假设哈,命令字是F3E2(16进制),数据是42 3A,那么我们异或校验的工作过程如下:

1:将命令字和数据组合起来:结果为F3 E2 42 3A(8位数据依次写开)

2:从第一个8位数据开始,将其与第二个8位进行异或操作,取得结果。即示例中F3与E2进行异或操作,计算过程如下:

       1111 0011(F3)

XOR    1110 0010 (E2)

结果:  0001 0001 (11)

3:将上次计算结果,与第三个8位数据进行异或操作,再次取得结果。即示例中11与42进行异或操作,计算过程如下:

       0001 0001 (11)

XOR    0100 0010 (42)

结果:  0101 0011(53)

3:再将上次计算结果,与其后的数据依次进行异或操作,最后就可以得到正确的异或校验结果啦。示例中53与最后一个8位数据3A进行异或操作,计算过程如下:

       0101 0011(53)

XOR    0011 1010 (3A)

结果:  01101001 (69)



0 0