tags:

views:

107

answers:

3

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

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?

+1  A: 

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.

Kirill V. Lyadvinsky
+7  A: 

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, i.e. 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.

AndreyT
Yes, yes :D Thank you. Yes!
Xaero
Err just one thing...this may sound irritating but i just need help in this tiny bit: i understood -1 in 2's complement is 11111111. but i am not able to calculate the reverse. Could you please help out?
Xaero
@Xaero: Reverse? The rule for negating a number in 2's complement is simple: invert all bits and add 1.
AndreyT
So if i do this - 1111 1111 ---invert all bits--- 0000 0000 --- add 1---- i get this 0000 0001. This is 1 right?
Xaero
@Xaero: Yes. You had `-1` and it became `+1`, as it should. Now you can do the same thing on `+1` and you'll get `-1` again.
AndreyT
This is so damn confusing :(
Xaero
A: 

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

Bart van Ingen Schenau