How Negative values are stored in system ????
int a = -1;
printf("Value of a:%x",a );
what will be the result ??? Can you tell me the value of it ???
Is it possible to predict this value ??
Result is:
Value of a:0xffff ffff
Yes it is possible to predict the value of it.
Reasoning:
Every bit is on or off / binary coded system. In binary coded system,
the negative values can be represented in two ways:
1.One's complement [convert the zeroes to ones and ones to zeroes]
2.Two's complement [ Two complement = one's complement value + 1]
value of 1: 0000 0001
One's complement to represent -1: 1111 1110 [changing the zeroes and to 1s and ones to zeroes in 0000 0001]
Two's complement value : 1111 1111 [1111 1110 +1]
To represent -1, the values will be stored as 1111 1111 in memory.
In my system, size(int) is 4 bytes.
To represent value 1 in Hex: 0x00000001 [ single value represents 4 bits].
Value 1 in binary : 00000000 00000000 00000000 00000001
One's complement for -1 in binary: 11111111 11111111 11111111 11111110
Two's complement for -1 in binary: 11111111 11111111 11111111 11111111
Two's complement for -1 in Hex : 0x ffff ffff [single digit represents 4 binary digits]
In the same way, we can try for different negative values too.
int a = -1;
printf("Value of a:%x",a );
what will be the result ??? Can you tell me the value of it ???
Is it possible to predict this value ??
Result is:
Value of a:0xffff ffff
Yes it is possible to predict the value of it.
Reasoning:
Every bit is on or off / binary coded system. In binary coded system,
the negative values can be represented in two ways:
1.One's complement [convert the zeroes to ones and ones to zeroes]
2.Two's complement [ Two complement = one's complement value + 1]
value of 1: 0000 0001
One's complement to represent -1: 1111 1110 [changing the zeroes and to 1s and ones to zeroes in 0000 0001]
Two's complement value : 1111 1111 [1111 1110 +1]
To represent -1, the values will be stored as 1111 1111 in memory.
In my system, size(int) is 4 bytes.
To represent value 1 in Hex: 0x00000001 [ single value represents 4 bits].
Value 1 in binary : 00000000 00000000 00000000 00000001
One's complement for -1 in binary: 11111111 11111111 11111111 11111110
Two's complement for -1 in binary: 11111111 11111111 11111111 11111111
Two's complement for -1 in Hex : 0x ffff ffff [single digit represents 4 binary digits]
In the same way, we can try for different negative values too.
Tuesday, February 22, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment