无法在react-native中创建无符号变量

时间:2019-06-16 22:39:48

标签: react-native

我找不到定义无符号变量的方法。 以下语法始终创建一个带符号的变量。 让MyExpectedFlag = 0; 当我根据参数执行SUM,“ AND”或“ OR”运算时,结果将变为负值。 当APP收到字符串消息时,将根据第一个参数将一个值分配给变量,其中每个位代表一个必须包含在消息其余部分中的参数。 MyExpectedFlag = 0x8006000f-检查第一个参数后 APP必须找到与位31、18、17、3、2、1,0有关的参数 找到参数后,我使用它们的指针来设置相关位。 MyGotFlag | = 1 <<指针;要么 MyGotFlag + = 1 <<指针;

1)情况1 让MyGotFlag = 0x80000000; 预期:2147483648实际:214748368-确定 MyGotFlag + = 0x40000000; 预期:3221225472实际:3221225472-确定 MyGotFlag&= 0xfff87fff; 预期:3221225472实际:-1073741824-nok

2)情况2 让MyGotFlag = 0; MyGotFlag | = 0x80000000; 预期:2147483648实际:-214748368-否

下面是要测试的示例代码:

    import React, {Component} from 'react';
    import {Platform, StyleSheet, Text, View} from 'react-native';

    const instructions = Platform.select({
      ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
      android: 'Double tap R on your keyboard to reload,\n' + 'Shake or press menu button for dev menu',
    });

    const responseStr = [
      'amb$',    0x80000000, // AMBIENT ID
      'acn$',         0,     // ACTION OF SCENE ID
      'bot$',         0,     // BUTTON ID
      'cr$',     0x29df8101, // Connection response
      'date$',        0,     // Date
      'db$',          0,     // DIMMER blink ( User )
      'dsp$',         0,     // DEVICE ID
      'dsr$',         0,     // Device Status response
      'evt$',         0,     // EVENT ID
      'ib$',          0,     // IRED blink ( User )
      'ic$',          0,     // Icone ID or IRED CODE ID
      'ip$',          0,     // IRED stop ( User )
      'kill$',        0,     // Reset CENTRAL setup to factory
      'ksr$',         0,     // KEYPAD Sensor response
      'level$',       0,     // RF LEVEL request
      'lrn$',         0,     // LEARN IRED process
      'mod$',         0,     // General Module ID
      'nok$',         0,     // Last Message sent by APP with ERROR
      'ok$',          0,     // Last Message sent by APP was correct
      'pd$',     0xc0078000, // Remote Module Pairing
      'rf$',          0,     // RF Channel of ATMEL modules
      'rb$',          0,     // RELAY blink ( User )
      'rp$',          0,     // Replace Module response
      'scan$',        0,     // Ongoing Process
      'setup$',       0,     // Status of CENTRAL or CENTRAL SETUP
      'sm$',          0,     // HW version - SMART
      'sr$',     0x20080000, // Scan Response
      'start$',       0,     // Started one process by CENTRAL
      'status$',      0,     // IRED CODE save by CENTRAL
      'tkp$',         0,     // KEYPAD KEY ID
      'uep$',         0,     // User ID
      'wb$',          0,     // RGBW blink ( User )
    ];

    let MsgSintaxRes = false; // Clear variable, Invalid Message recvd
    let ArgExpected = 0; // Expected arguments according to responseStr
    let ArgPresFlag = 0; // Arguments received flag
    let ArgExpected1 = 0;// Expected arguments according to responseStr
    let ArgPresFlag1 = 0;   // Arguments received flag
    let Result = '';
    let Result1 = '';

    type Props = {};
    export default class App extends Component<Props> {

      componentWillMount() {
      // Message to be checked:  "pd$status$nok$" to be checked
      //
      // Case 1
      //
      // Simulates that found "pd"
        ArgExpected = responseStr[39];// Gets value from array
      // ArgExpected should be 0xc0078000 or decimal 3221716992 - OK
      // Simulates that found "status"
      // Sets bit related to the Argument Identifier
        ArgPresFlag |= 0x40000000;
      // ArgPresFlag should be 0x40000000 or decimal 1073741824 - OK
      // Simulates that found "nok"
      // Sets bit related to the Argument Identifier
        ArgPresFlag |= 0x80000000;
      // Expected to be 0xc0000000 but the result is 0xffffffffc0000000 
      // dec -1073741824 - ERROR
      // Comparison should be OK, but it isn't
        if ( ArgExpected &&
             ArgExpected === ArgPresFlag ) {// Got all the Arguments???
          MsgSintaxRes = true;         // Sets flag, Message is valid
        }
        Result = 'Exp: ' + ArgExpected + ' ' + 'Calc: ' + ArgPresFlag;

      //
      // Case 2 - Farei um operacao & com ArgExpected, aí funciona, 
      //          situacoes, que nao farei o & com ArgExpected
      // Simulates that found "pd"
        ArgExpected1 = responseStr[39];// Gets value from array
      // ArgExpected should be 0xc0078000 or decimal 3221716992 - OK
      // Simulates that found "status"
        ArgPresFlag1 |= 0x40000000;// Sets bit
      // ArgPresFlag should be 0x40000000 or decimal 1073741824 - OK
      // Simulates that found "nok"
        ArgPresFlag1 |= 0x80000000;// Sets bit
      // Expected to be 0xc0000000 but the result is 0xffffffffc0000000 
      // dec -1073741824 - ERROR
      // Clear bits that represents the Module Code
        ArgExpected1 &= 0xfff87fff; 
      // Expected to be 0xc0000000 but the result is 0xffffffffc0000000 
      // dec -1073741824 - ERROR
      // Both value ArgExpected and ArgPresFlag are negative (error)
      // Comparison should be OK
        if ( ArgExpected1 &&
             ArgExpected1 === ArgPresFlag1 ) {// Got all Arguments???
          MsgSintaxRes = true; // Sets flag, Message is valid
        }
        Result1 = 'Exp: ' + ArgExpected1.toString() + ' ' + 'Calc: ' + ArgPresFlag1.toString();
      }

      render() {
        return (
          <View style={styles.container}>
            <Text style={styles.welcome}>Welcome to React Native!</Text>
            <Text style={styles.instructions}>{Result}</Text>
            <Text style={styles.instructions}>{Result1}</Text>
          </View>
        );
      }
    }


    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },
      welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
      },
      instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
      },
    });

0 个答案:

没有答案