理解c中的“按位 – 和(&)”和“一元补码(〜)”

理解c中的“按位 – 和(&)”和“一元补码(〜)”,第1张

概述当在这段代码中使用Bitwise-And和Unary Complement时,我有点麻烦 if((oldByte==m_DLE) & (newByte==m_STX)) { int data_index=0; //This below line --- does it returns true if both the oldByte and newByte are not true 当在这段代码中使用Bitwise-And和Unary Complement时,我有点麻烦

if((oldByte==m_DLE) & (newByte==m_STX)) {    int data_index=0;   //This below line --- does it returns true if both the oldByte and newByte are not true    //and within timeout while((timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))) {                         if(Serial.available()>0) {                            oldByte=newByte;                            newByte=Serial.read();                            if(newByte==m_DLE) {                            .                            .                            .

是 *** 作符和 *** 作符〜正在执行逻辑非 *** 作,如检查,直到oldByte和newByte都为false

以上代码来自link – >第227行代码

我试图在C中使用我的应用程序的代码实现,但没有计时功能

if((oldByte==DLE) && (newByte== STX)) {    data_index = 0;     // is this the correct implematation for above C++ code to C      while(! ((oldByte== DLE) && (newByte== ETX))){          oldByte = newByte;

这种方法在C中实现是否正确

解决方法
(timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))

相当于(但可能不太可读)

(timeout.read_s()<m_timeout) && !(oldByte==m_DLE && newByte==m_ETX)

相当于(和IMO的可读性不如)

(timeout.read_s()<m_timeout) && (oldByte!=m_DLE || newByte!=m_ETX)

编辑:应该添加一个关于短路的警告.虽然特定的示例语句都将返回相同的值,但使用&&或||将跳过评估不会影响结果的部分.这在您的具体示例中并不重要,但在以下示例中可能非常重要:

(oldByte!=nullptr & *oldByte == m_ETX) // will crash when oldByte=nullptr.(oldByte!=nullptr && *oldByte == m_ETX) // will evaluate to false when oldByte=nullptr.
总结

以上是内存溢出为你收集整理的理解c中的“按位 – 和(&)”和“一元补码(〜)”全部内容,希望文章能够帮你解决理解c中的“按位 – 和(&)”和“一元补码(〜)”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1217035.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存