简体   繁体   English

我似乎不理解该程序关于将整数指针转换为字符指针的输出

[英]I don't seem to understand the output of this program regarding conversion of integer pointer to character pointer

In the program below i initiliaze i to 255 Thus in Binary we have: 在下面的程序我initiliaze 255因此,在二进制,我们有:

0000 0000 1111 1111

That is in Hex: 这是在十六进制:

0x 0 0 ff 0x 0 0 ff

So according to Little-Endian layout: The Lower Order Byte - 0xff is stored first. 因此根据Little-Endian布局:首先存储低位字节 - 0xff


#include<cstdio>
int main()
{
int i=0x00ff; //I know 0xff works. Just tried to make it understable
char *cptr=(char *)&i;
if(*(cptr)==-127)
printf("Little-Endian");
else
printf("Big-Endian");

}

So, when i store the address of i in cptr it should point to the Lower Byte (assuming Little Endian, coz that is what my System has) . 所以,当我的地址存储在CPTR应该指向低字节(假设小端,怎么这是我的系统了)。

Hence, *cptr contains 1111 1111 . 因此, * cptr包含1111 1111 This should come down to -127. 这应该降到-127。 Since, 1 bit is for the Sign-bit. 因为, 1位用于符号位。

But when i print *cptr 's value i get -1 , why is it so? 但是当我打印* cptr的值时我得到-1 ,为什么会这样呢?

Please explain where am i going wrong? 请解释我哪里出错了?

Where did you get the idea that 1111 1111 is -127 ? 你在哪里知道1111 1111-127 Apparently, you are assuming that the "sign bit" should be interpreted independently from the rest of the bits, ie setting the sign bit in binary representation of +127 should turn it into -127 , right? 显然,您假设“符号位”应该独立于其余位进行解释,即将符号位设置为+127二进制表示应该将其转换为-127 ,对吧? Well, a representation that works that way does indeed exist: it is called Signed Magnitude representation. 那么,以这种方式工作的表示确实存在:它被称为Signed Magnitude表示。 However, it is virtually unused in practice. 但是,它在实践中几乎未被使用。 Most modern machines use 2's Complement representation for signed types, which is a completely different thing. 大多数现代机器使用2的补码表示来表示签名类型,这是完全不同的事情。

On a 2's-complement machine 1111 1111 has always been -1 . 在二进制补码机1111 1111一直是-1 -127 would be 1000 0001 . -127将是1000 0001 And 1000 0000 is -128 , BTW. 并且1000 0000-128 ,BTW。

On top of that keep in mind that char type is not necessarily signed. 最重要的是要记住, char类型不一定是签名的。 If you specifically need a signed type, you have to spell it out: signed char . 如果您特别需要签名类型,则必须拼写出来: signed char

1111 1111 is a -1 because -1 is the largest negative integral number. 1111 1111-1因为-1是最大的负整数。 Remember that -1 is more then -2 in math, so binary representation should have the same properties for the convenience. 请记住, -1在数学中大于-2 ,因此为方便起见,二进制表示应具有相同的属性。 So 1000 0001 will represent -127 . 因此1000 0001将代表-127

There are three ways to represent negative numbers in binary: Signed magnitude, ones-complement and twos-complement. 有三种方法可以用二进制表示负数:有符号幅度,1补码和2补码。

The signed magnitude is easiest to understand: One bit is used to represent the sign (0=+, 1=-), the other bits represent the magnitude of the value. 有符号的幅度最容易理解:一位用于表示符号(0 = +,1 = - ),其他位表示值的大小。
In ones-complement, a negative value is obtained by performing a bitwise inversion on the corresponding positive number (toggle all bits) 在补码中,通过对相应的正数进行逐位求逆来获得负值(切换所有位)
In twos-complement, the way to convert between positive and negative numbers is less straightforward (flip all bits, then add 1), but it has some characteristics that make it particularly useful in computers. 在二进制补码中,正数和负数之间转换的方式不那么简单(翻转所有位,然后加1),但它具有一些特性,使其在计算机中特别有用。

Because computers work very well with twos-complement representations, that is what gets used in most computers for representing integers. 因为计算机在二进制补码表示中运行良好,所以大多数计算机都使用它来表示整数。

What is going wrong is that you expected a signed magnitude representation (highest bit set, so negative value. All other bits set as well, so value = -127), but the computer is using twos-complement representation (where all bis set == -1). 出了什么问题是你期望一个有符号的幅度表示(最高位设置,所以负值。所有其他位也设置,所以值= -127),但计算机使用二进制补码表示(其中所有bis set = = -1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM