C
cylin
Hi all,
If I have a variable and it's just have 22 bits,
and I must set this variable unsigned integer.
This 2's complement value maybe is negative.
For example:
A= 11 1111 1111 1110 1100 0000 = 4193984
3 f f e c 0
A's 2's complement should be
00 0000 0000 0001 0100 0000 = 320
I try to write a sample code below:
----------------------------------------------------
unsigned int A=4193984;
unsigned char Byte2=~(((A & 0x00ff0000) >> 16)+192); // 192 is sum of
the first 2 bits of one byte.
unsigned char Byte3=~((A & 0x0000ff00) >> 8);
unsigned char Byte4=~(A & 0x000000ff);
unsigned int ATwoSComplement=(Byte2 << 16) + (Byte3 << 8) + Byte4 + 1;
----------------------------------------------------
But when some variable's 2's complement is negative, this code fails.
How to find it's 2's complement value correctly?
Thanks for your help.
Regards,
cylin.
If I have a variable and it's just have 22 bits,
and I must set this variable unsigned integer.
This 2's complement value maybe is negative.
For example:
A= 11 1111 1111 1110 1100 0000 = 4193984
3 f f e c 0
A's 2's complement should be
00 0000 0000 0001 0100 0000 = 320
I try to write a sample code below:
----------------------------------------------------
unsigned int A=4193984;
unsigned char Byte2=~(((A & 0x00ff0000) >> 16)+192); // 192 is sum of
the first 2 bits of one byte.
unsigned char Byte3=~((A & 0x0000ff00) >> 8);
unsigned char Byte4=~(A & 0x000000ff);
unsigned int ATwoSComplement=(Byte2 << 16) + (Byte3 << 8) + Byte4 + 1;
----------------------------------------------------
But when some variable's 2's complement is negative, this code fails.
How to find it's 2's complement value correctly?
Thanks for your help.
Regards,
cylin.