unsigned long int 的陷阱

本文探讨了在使用无符号整型变量进行减法运算时出现的意外结果问题,并通过一个具体的程序示例展示了如何通过类型转换来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天写了个程序,纠结好久,代码如下:
UINT32 number_user= 20 , voltage= 300;
if((number_user - voltage) >= 30)//电压比预计的电压要大(允许有30mv的误差)
  {
          Frequence = Frequence + 100;//加大开关频率
          if(Frequence >= 40000)//PWM的最大频率限制在40K
          {
                Frequence = 40000;
          }
  }
 else if((voltage - number_user) >= 30)
 {
       Frequence = Frequence - 100;//加大开关频率
       if(Frequence <= 1000)
        {
               Frequence = 1000;
        }
 }
这个程序很奇怪,一直在if里面执行,但是我们可以知道number_user – voltage = –180 怎么可能大于30呢???
但是。。。。这里是两个无符号长整型进行相减,得出的结果仍然是无符号长整型,这就是说结果并不是-180 ,而是
4294967116l,想想这是一个多大的数,怎么可能比30 小!!!!!
然后我将代码改成了:
UINT32 number_user= 20 , voltage= 300;
if ((int)(number_user - voltage) >= 30)//电压比预计的电压要大(允许有30mv的误差)
{
    Frequence = Frequence + 100;//加大开关频率
    if (Frequence >= 40000)//PWM的最大频率限制在40K
    {
        Frequence = 40000;
    }
}
else if ((int)(voltage - number_user) >= 30)
{
    Frequence = Frequence - 100;//加大开关频率
    if (Frequence <= 1000)
    {
        Frequence = 1000;
    }
}
将结果强制转换成有符号的数据!ok,又解决一个问题,O(∩_∩)O~
void CameraDeviceSession::updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove) { 1. lock_acquire: 调用 Autolock 将获取锁 this->mInflightLock.mMutex。[显示详情] 1107 Mutex::Autolock _l(mInflightLock); 2. 对 cachesToRemove 的另一元素迭代。 5. 对 cachesToRemove 的另一元素迭代。 1108 for (auto& cache : cachesToRemove) { 1109 auto cbsIt = mCirculatingBuffers.find(cache.streamId); 3. 条件 cbsIt == std::__1::map<int, std::__1::unordered_map<unsigned long long, native_handle const *, std::__1::hash<unsigned long long>, std::__1::equal_to<unsigned long long>, std::__1::allocator<std::__1::pair<unsigned long long const, native_handle const *> > >, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, std::__1::unordered_map<unsigned long long, native_handle const *, std::__1::hash<unsigned long long>, std::__1::equal_to<unsigned long long>, std::__1::allocator<std::__1::pair<unsigned long long const, native_handle const *> > > > > >::iterator const(this->mCirculatingBuffers.end()),使用了 true 分支。 6. 条件 cbsIt == std::__1::map<int, std::__1::unordered_map<unsigned long long, native_handle const *, std::__1::hash<unsigned long long>, std::__1::equal_to<unsigned long long>, std::__1::allocator<std::__1::pair<unsigned long long const, native_handle const *> > >, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, std::__1::unordered_map<unsigned long long, native_handle const *, std::__1::hash<unsigned long long>, std::__1::equal_to<unsigned long long>, std::__1::allocator<std::__1::pair<unsigned long long const, native_handle const *> > > > > >::iterator const(this->mCirculatingBuffers.end()),使用了 false 分支。 1110 if (cbsIt == mCirculatingBuffers.end()) { 1111 // The stream could have been removed 4. 继续循环 1112 continue; 1113 } 1114 CirculatingBuffers& cbs = cbsIt->second; 1115 auto it = cbs.find(cache.bufferId); 7. 条件 it != std::__1::unordered_map<unsigned long long, native_handle const *, std::__1::hash<unsigned long long>, std::__1::equal_to<unsigned long long>, std::__1::allocator<std::__1::pair<unsigned long long const, native_handle const *> > >::iterator const(cbs->end()),使用了 true 分支。 1116 if (it != cbs.end()) { CID 171518: (#1 of 1): 持有锁时等待 (SLEEP) 8. sleep: 对 freeBuffer 的调用可能在持有锁 this->mInflightLock.mMutex 的情况下睡眠。[显示详情] 1117 sHandleImporter.freeBuffer(it->second); 1118 cbs.erase(it); 1119 } else { 1120 ALOGE("%s: stream %d buffer %" PRIu64 " is not cached", 1121 __FUNCTION__, cache.streamId, cache.bufferId); 1122 } 1123 } 1124
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值