hixon 发表于 2022-9-6 12:56:03

位操作

本帖最后由 hixon 于 2022-9-6 12:58 编辑

1、获取某位:移位和与操作
device.io.com0.onReceive = function(count) {
    var data = this.read();
    // 通过移位和与操作获取位
    var bit0 = data & 0x01;
    var bit1 = (data >> 1) & 0x01;
    var bit7 = (data >> 7) & 0x01;
    // 通过String.toString(2)方法,以二进制显示data的值
    util.console.log('data ' + data.toString(2));
    util.console.log('bit0: ' + bit0);
    util.console.log('bit1: ' + bit1);
    util.console.log('bit7: ' + bit7);
};2、设置某位为0:用与操作
ui.main.textButton.onRelease = function() {
    var value = 0xFF;
    util.console.log('value1: '+ value.toString(2)); // 11111111
    value &= 0xF7; // 修改第3位(最低位是第0位)的值为0
    util.console.log('value2: '+ value.toString(2)); // 11110111
};
3、设置某位为1:用或操作
ui.main.textButton.onRelease = function() {
    var value = 0xE0;
    util.console.log('value1: '+ value.toString(2)); // 11100000
    value |= 0x08; // 修改第3位(最低位是第0位)的值为1
    util.console.log('value2: '+ value.toString(2)); // 11101000
};

页: [1]
查看完整版本: 位操作