标签: unsigned

内存如何存储有符号和无符号字符

刚开始学C,感觉有点迷茫。

我有一些问题:

  1. 如果我有以下代码:
signed  char x = 56;
// ?In the RAM, I will see 00111000? yes/no?

signed char z = -56;
// In the RAM, I will see 11001000 yes/no?

unsigned char y = 200;
// ?In the RAM, I will see 11001000? yes/no?
Run Code Online (Sandbox Code Playgroud)
  1. 我有以下代码:
if (z<0){
   printf("0 is bigger then z ");
}
Run Code Online (Sandbox Code Playgroud)

编译后,汇编指令如何知道z是-56而不是200?(有符号和无符号的特殊ASM指令?)。

正如我在问题 1 中提到的,z 和 y 的值是 11001000,并且没有任何指示知道它是有符号还是无符号。

如果我没有找到正确的提问方式,请原谅,希望你理解我 谢谢

c assembly unsigned signed char

3
推荐指数
1
解决办法
514
查看次数

运行时错误:有符号整数溢出:2 * 2147483647 无法以“int”类型表示

我正在尝试运行以下代码:

 int across(vector<int> &nums, int l , int m, int h){
 
        
        int i = l, j = m +1;
        int count = 0;
        unsigned long long int n;
        unsigned long long int prev;
        
        while(i <= m && j <=h){
            n = 2* nums[j];
            prev = nums[i];
            if(prev > n){
                count += m - i +1;
                j++;
            }
            else{
                i++;
            }
        }
        
        return count;
    }
Run Code Online (Sandbox Code Playgroud)

nums 是一个从索引 l 到 m 以及从索引 m+1 到 h 排序的向量。我必须计算对(i,j)的数量,这样nums[i] > 2 * nums[j]

当其中一个元素为 …

c++ unsigned signed runtime-error runtime

3
推荐指数
1
解决办法
5192
查看次数

使用ruby将存储为signed的无符号整数转换回原始值

一个C程序将它认为是64位无符号整数的东西放入Postgres数据库中的一个列,该数据库被输入为int8.对于Postgres,int8总是'signed int8'(没有像'unsigned int8'那样).因此,我所使用的Ruby程序显示从该空间的上半部分中的Postgres检索到的数字为负数.

在Ruby中,采用-ve整数并将其转换为C程序员所期望的64位无符号整数的正确方法是什么?

ruby postgresql unsigned signed

2
推荐指数
2
解决办法
3726
查看次数

具有32位寄存器的双精度整数减法(MIPS)

我正在学习计算机算术.我使用的书(Patterson和Hennessey)列出了以下问题.

写mips代码以对64位数据进行双精度整数减法.假设第一个操作数在寄存器$ t4(hi)和$ t5(lo)中,第二个在$ t6(hi)和$ t7(lo)中.

我对答案的解决方案是

sub  $t3, $t5, $t7   # Subtract lo parts of operands. t3 = t5 - t7
sltu $t2, $t5, $t7   # If the lo part of the 1st operand is less than the 2nd,
                     #   it means a borrow must be made from the hi part
add  $t6, $t6, $t2   # Simulate the borrow of the msb-of-low from lsb-of-high
sub  $t2, $t4, $t6   # Subtract the hi's. t2 = t4 - t6
Run Code Online (Sandbox Code Playgroud)

然而,作者给出了针对该问题的解决方案如下

对于有符号的双精度整数, …

unsigned signed mips integer-overflow double-precision

2
推荐指数
1
解决办法
5069
查看次数

转换unsigned int时,GCC如何使用fildll?

英特尔指令集参考声明(强调我的):

将有符号整数源操作数转换为双扩展精度浮点格式,并将该值压入FPU寄存器堆栈.源操作数可以是字,双字或四字整数.加载时没有舍入错误.源操作数的符号被保留.该指令的操作在非64位模式和64位模式下是相同的.

在这里你可以看到我的测试用例:

% cat stackoverflow.c       
float uint2float(unsigned int a) {
    return a;
}
% gcc -c stackoverflow.c    
% objdump -d stackoverflow.o

stackoverflow.o:     file format elf32-i386


Disassembly of section .text:

00000000 <uint2float>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 08                sub    $0x8,%esp
   6:   8b 45 08                mov    0x8(%ebp),%eax
   9:   ba 00 00 00 00          mov    $0x0,%edx
   e:   89 45 f8                mov    %eax,-0x8(%ebp)
  11:   89 55 fc                mov    %edx,-0x4(%ebp)
  14:   df 6d f8                fildll …
Run Code Online (Sandbox Code Playgroud)

floating-point x86 assembly unsigned x87

2
推荐指数
1
解决办法
457
查看次数

按位运算的有符号和无符号之间的差异

按位运算的有符号和无符号变量之间有什么区别吗?
例如,处理无符号数时:
AND 00000111, 00001101
将导致00000101.

但是在处理签名号码时会发生什么?

unsigned signed bit-manipulation bitwise-operators

2
推荐指数
1
解决办法
5313
查看次数

VHDL std_logic_vector转换为带有numeric_std的signed和unsigned

我对使用转换的一些疑惑std_logic_vector到signed/ unsigned.我总是使用转换signed(...),unsigned(...)但是当我尝试使用库numeric_std(to_signed,to unsigned)中定义的转换时,它不起作用.有人可以向我解释为什么会这样吗?为什么转换unsigned()和signed()工作?

unsigned signed vhdl

2
推荐指数
1
解决办法
5万
查看次数

为什么<< 32在javascript中不会导致0?

这是假的:

(0xffffffff << 31 << 1) === (0xffffffff << 32)
Run Code Online (Sandbox Code Playgroud)

看起来应该是真的.添加>>> 0任何地方都不会改变这一点

为什么这样,我怎样才能正确编写处理的代码<< 32?

javascript unsigned bit-manipulation integer-overflow bit-shift

2
推荐指数
1
解决办法
156
查看次数

禁止隐式`unsigned`到'double`转换

是否有可能禁止C++中基本类型之间的隐式转换?特别是,我想禁止隐式转换unsigned为float或者double因为这些错误:

int i = -5;
...
unsigned u = i; // The dawn of the trouble.
...
double d = u;   // The epicenter of the bug that took a day to fix.
Run Code Online (Sandbox Code Playgroud)

我试过这样的事情:

explicit operator double( unsigned );
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用:

explicit.cpp:1: error: only declarations of constructors can be ‘explicit’
explicit.cpp:1: error: ‘operator double(unsigned int)’ must be a nonstatic member function
Run Code Online (Sandbox Code Playgroud)

c++ floating-point unsigned

2
推荐指数
1
解决办法
89
查看次数

C:qsort似乎不适用于unsigned long

谁能告诉我以下示例有什么问题?我把它从这里拿走并换成int了unsigned long.我也改变了cmpfunc正确处理unsigned long.

#include <stdio.h>
#include <stdlib.h>

unsigned long values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
  if(*(unsigned long*)a - *(unsigned long*)b < 0){
    return -1;
  }

  if(*(unsigned long*)a - *(unsigned long*)b > 0){
    return 1;
  }

  if(*(unsigned long*)a - *(unsigned long*)b == 0){
    return 0;
  }
}

int main()
{
   int n;

   printf("Before sorting the list is: …
Run Code Online (Sandbox Code Playgroud)

c unsigned qsort

2
推荐指数
1
解决办法
643
查看次数