简体   繁体   中英

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:

0000 0000 1111 1111

That is in Hex:

0x 0 0 ff

So according to Little-Endian layout: The Lower Order Byte - 0xff is stored first.


#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) .

Hence, *cptr contains 1111 1111 . This should come down to -127. Since, 1 bit is for the Sign-bit.

But when i print *cptr 's value i get -1 , why is it so?

Please explain where am i going wrong?

Where did you get the idea that 1111 1111 is -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? Well, a representation that works that way does indeed exist: it is called Signed Magnitude representation. However, it is virtually unused in practice. Most modern machines use 2's Complement representation for signed types, which is a completely different thing.

On a 2's-complement machine 1111 1111 has always been -1 . -127 would be 1000 0001 . And 1000 0000 is -128 , BTW.

On top of that keep in mind that char type is not necessarily signed. If you specifically need a signed type, you have to spell it out: signed char .

1111 1111 is a -1 because -1 is the largest negative integral number. Remember that -1 is more then -2 in math, so binary representation should have the same properties for the convenience. So 1000 0001 will represent -127 .

There are three ways to represent negative numbers in binary: Signed magnitude, ones-complement and twos-complement.

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.
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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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