标签: twos-complement

签名十六进制字符串到long int函数

我需要一个函数将32位或24位带符号(二进制补码)十六进制字符串转换为long int.需要在32位和64位机器上工作(无论长int的大小),无论机器是否是双补机,都需要工作.

解:

long int hex2li (char hexStr[], int signedHex)
{
   int bits = strlen (hexStr) * 4;

   char *pEnd;
   long long int result = strtoll (hexStr, &pEnd, 16);

   if (pEnd[0] == '\0')
   {
      if (signedHex)
      {
         if (result >= (1LL << (bits - 1))) result -= (1LL << bits);
      }

      return (long int) result;
   }

   return LONG_MIN;
}
Run Code Online (Sandbox Code Playgroud)

c string hex twos-complement

0
推荐指数
1
解决办法
2700
查看次数

为什么偏向负数?

如果在我的编译器上,int是16位,那么它的范围是-32768到32767(在2的补码机器中).
我想知道为什么负数有1个额外的没有.即正数转为32767但负数转为另一个ie-32768.

-32768如何用2的补码m/c表示?

integer twos-complement

0
推荐指数
1
解决办法
197
查看次数

C 在内部使用哪个补码?

我查找的所有内容只是告诉我如何在 C 中进行补充运算/计算。

我想知道 C 在内部使用什么表示以及它如何处理溢出。

c twos-complement ones-complement

0
推荐指数
1
解决办法
94
查看次数

Two's Complement Division Algorithm (Intel)

On Intel machines, integer division requires a sequence of two instructions:

\n
    \n
  1. After you store the dividend in the a register (e.g., %eax), you extend the sign of the dividend into the corresponding d register (%edx).
  2. \n
  3. The idiv or div instruction itself then performs the division and places the quotient in the a register and the remainder in the d register.\nBut why does the operation require the sign extension? What does the algorithm actually do …

x86 assembly cpu-architecture division twos-complement

0
推荐指数
1
解决办法
336
查看次数

在两个补语中截断?

我很难理解从unsigned转换为Two的Complement时截断是如何工作的.有人可以解释一下吗?(我的textook使用将4位值截断为3位值的示例,并说-1变为-1,但-5变为3).

c binary integer truncation twos-complement

-1
推荐指数
1
解决办法
3817
查看次数

如何仅使用 C 中的按位运算符来检查值是否大于 7?

对于此问题,如果参数大于 7,则要求返回 1,否则返回 0。

例如,如果 x 为 8,则该函数将返回 1。如果 x 为 7,则该函数将返回 0。

唯一允许的合法运算符是 (! ~ & ^ | + << >>),它禁止使用其他任何运算符,例如 -、for 循环、while 循环、if 语句等。

我们可以假设系统使用 2 的补码和整数的 32 位表示,以算术方式执行右移,并且在将整数移位超过字大小时会出现不可预测的行为。

我知道不使用 - 操作的减法可以用 ~ 来完成,但说实话,我不知道如何从逻辑上思考这个问题。

c bit-manipulation bitwise-operators twos-complement

-1
推荐指数
1
解决办法
540
查看次数

关系运算符精度

我有一个关于关系运算符的问题,它们总是给出正确的结果吗?因为如果我们运行这行代码,结果将是 1 而不是 0

cout<<(ULLONG_MAX==-1);
Run Code Online (Sandbox Code Playgroud)

如果我们继续两边都减 1,结果仍然是 1

所以这可能会在我们的程序中产生错误的结果

解决办法是什么?

c++ integer twos-complement relational-operators

-1
推荐指数
1
解决办法
103
查看次数