有符号数和无符号数差异 [英] Signed number and unsigned number differences

查看:102
本文介绍了有符号数和无符号数差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解处理器在执行指令时不知道号码是否已签名。在打印数字的同时,标志进入画面。假设我使用%d,则数字将被解释为有符号,并且将根据数字的2的补码表示打印输出。以上是我的理解,如果我错了,请纠正我。



我的疑问是,签名字符和unsigned字符之间的区别是什么(让'简单地使用字符'如果我打算在我的程序中使用,那么8位)



例如。



I understood processor don't know whether the number is signed or not while executing the instruction. While printing the number alone the sign get into picture. Say if i use %d then the number will be interpreted as signed and output will be printed based on the 2's complement representation of the number. Above is my understanding , please correct me if I am wrong.

My doubt is, what is the difference between signed char and unsigned char(let' take char for simplicity since 8 bit) if I am going to use in my program.

for example.

signed char ch = 200; 





如果我将此'ch'用作数组索引,如下所示。



If I use this 'ch' as an array index as below.

int arr[210]=  {0}; //initialize all 210 to zero
arr[ch]='H';  //Here ch will be 200  or -56 ???

printf("val = %c\r\n", arr[ch])  //output printed is 'H'; 200th position is set to 'H'



基于输出我看到编译器没有采取arr [-56],它采取的是arr [200] 。



因为签名字符范围是-128到+127。因此200将被解释为-56(通过%d打印时)。但是在内存中,任何方式的值都是11001000.

因此,如果我的程序不打印该值,那么使用signed vs unsigned char没有区别。这种理解对吗?或者我在这里混淆了什么?请澄清。


based on the output i see compiler is not taking arr[-56], it is taking arr[200].

since it is a signed char range is from -128 to +127. so 200 will be interpreted as -56(while printing via %d). But in memory any way the value will be 11001000.
so if my program is not going to print the value then there is no differnce in using signed vs unsigned char . Is this understanding right? Or Am i confusing something here?Please clarify.

推荐答案

引用:

为基础在输出上我看到编译器没有采取arr [-56],它采取arr [200]。

based on the output i see compiler is not taking arr[-56], it is taking arr[200].

对不起,这是错误的结论。



要获得正确的输出,请参阅

Sorry, that is the wrong conclusion.

To get the correct one, see the output of

printf("val = %c\r\n", arr[200]); 


ch 的值为-56。您可以通过打印来检查:

The value of ch will be -56. You can check that by printing it:
printf("ch = %d\n", ch);



如果您想确定可以打印地址:


If you want to be really sure you can print the address:

printf("addr of arr: %p, addr of arr[h]: %p\n", arr, &arr[ch]);



输出可能如下所示:


The output might look like this:

addr of arr: 0xbe8db170, addr of arr[h]: 0xbe8db090





作为结果,数组被访问超出范围。启用所有编译器警告时,您应该收到相应的警告。



您假设 arr [200] 是这行中的'H'是错误的:



As a result, the array is accessed out of bounds. When enabling all compiler warnings, you should get an appropriate warning.

Your assumption that arr[200] is 'H' in this line is wrong:

printf("val = %c\r\n", arr[ch])  //output printed is 'H'; 200th position is set to 'H'



这可以通过传递-56和200而不是 ch


处理器不知道值是否已签名。这个概念完全构建在您的代码中。这就是你如何处理决定价值是否会被签署的价值的方式。唯一的问题是您必须在代码中始终将其视为特定值类型。
The processor doesn't know if the value is signed or not. That concept is built entirely into your code. It's how YOU treat the value that determines if the value is going to be signed or not. The only thing is is that YOU have to treat it as a certain value type consistently in your code.


这篇关于有符号数和无符号数差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆