减去无符号并获得签名

时间:2013-02-07 15:20:39

标签: c++ c

我可以用这种方式进行演员表吗?

int clockOffset;

clockOffset=((int64_t)Time_1_delayed-Time_1-Time_2_delayed+Time_2)/2;

其中所有时间变量均为uint

我希望将签名结果放在clockOffset

2 个答案:

答案 0 :(得分:1)

以下是您可以在C中执行的操作(假设整数和无符号整数比长整数短):

#include <limits.h>

...
  unsigned int Time_1_delayed, Time_1, Time_2_delayed, Time_2;
...
  long long diff = ((long long)Time_1_delayed - Time_1 - Time_2_delayed + Time_2) / 2;
  int clockOffset;

  if (diff < INT_MIN || diff > INT_MAX)
    HandleTheError();

  clockOffset = diff;
...

答案 1 :(得分:0)

你不需要施放。例如,你可以这样做。

uint32_t a = 100;
uint32_t b = 200;
int32_t c = a - b;

你会得到c = -100