简体   繁体   English

确定16位二进制数是负数还是正数

[英]Determining if a 16 bit binary number is negative or positive

I'm creating a library for a temperature sensor that has a 16-bit value in binary that is being returned. 我正在为温度传感器创建一个库,该库具有返回的16位二进制值。 I'm trying to find the best way to check if that value returned is negative or positive. 我试图找到最好的方法来检查返回的值是负还是正。 I'm curious as to whether or not I can check if the most significant bit is a 1 or a 0 and if that would be the best way to go about it, how to successfully implement it. 我很好奇我是否可以检查最高有效位是1还是0,以及这是否是实现它的最佳方法以及如何成功实现它。

I know that I can convert it to decimal and check that way but I just was curious if there was an easier way. 我知道我可以将其转换为十进制格式并进行检查,但是我只是好奇是否有更简单的方法。 I've seen it implemented with shifting values but I don't fully understand that method. 我已经看到它是通过移动值来实现的,但是我并不完全了解该方法。 (I'm super new to c++) (我是C ++的新手)

float TMP117::readTempC(void)
{
    int16_t digitalTemp;      // Temperature stored in the TMP117 register

    digitalTemp = readRegister(TEMP_RESULT); //Reads the temperature from the sensor

    // Check if the value is a negative number
    /*Insert code to check here*/

    // Returns the digital temperature value multiplied by the resolution
    // Resolution = .0078125
    return digitalTemp*0.0078125; 
}

I'm not sure how to check if the code works and I haven't been able to compile it and run it on the device because the new PCB design and sensor has not come in the mail yet. 我不确定如何检查代码是否有效,并且还无法编译并在设备上运行它,因为新的PCB设计和传感器尚未寄出。

I know that I can convert it to decimal and check that way 我知道我可以将其转换为十进制并进行检查

I am not sure what you mean. 我不确定你的意思。 An integer is an integer, it is an arithmetic object you just compare it with zero: 整数是整数,它是一个算术对象,只需将其与零进行比较:

if( digitalTemp < 0 )
{
    // negative
}
else
{
    // positive
}

You can as you suggest test the MSB, but there is no particular benefit, it lacks clarity, and will break or need modification if the type of digitalTemp changes. 您可以按照您的建议测试MSB,但是并没有什么特别的好处,它缺乏清晰度,并且如果digitalTemp的类型发生更改,它将被破坏或需要进行修改。

if( (digitalTemp & 0x8000 )
{
    // negative
}
else
{
    // positive
}

"conversion to decimal", can only be interpreted as conversion to a decimal string representation of an integer , which does not make your task any simpler, and is entirely unnecessary. “转换为十进制”只能解释为转换为整数的十进制字符串表示形式 ,这不会使您的任务更简单,并且完全没有必要。

I'm not sure how to check if the code works and I haven't been able to compile it and run it on the device because the new PCB design and sensor has not come in the mail yet. 我不确定如何检查代码是否有效,并且还无法编译并在设备上运行它,因为新的PCB设计和传感器尚未寄出。

Compile and run it on a PC in a test harness with stubs for teh hardware dependent functions. 带有存根的测试工具可以在PC上编译并运行它,以实现与硬件相关的功能。 Frankly if you are new to C++, you are perhaps better off practising the fundamentals in a PC environment with generally better debug facilities and faster development/test iteration in any case. 坦率地说,如果您是C ++的新手,那么在PC环境中实践基础知识可能会更好,无论如何,它们通常具有更好的调试功能和更快的开发/测试迭代。

In general 一般来说

float TMP117::readTempC(void)
{
    int16_t digitalTemp;      // Temperature stored in the TMP117 register

    digitalTemp = readRegister(TEMP_RESULT); //Reads the temperature from the sensor

    // Check if the value is a negative number
    if (digitalTemp < 0)
    {
         printf("Dang it is cold\n");
    }


    // Returns the digital temperature value multiplied by the resolution
    // Resolution = .0078125
    return digitalTemp*0.0078125; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM