This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

problem with 8052 addtion

Hi,
please look into this code
Code:
#include "REG52.h"
#include "intrins.h"

void main()
{ long int data1 = 0xFF000000;
char shift = 0xA0; data1 = _lrol_(data1,8); data1 = data1 & 0xFFFFFF00; data1 = data1 + shift;
}

the value of data1 i expect is 0x000000A0, but when i execute the above code, it shows a value of data1 = 0xFFFFFFA0. I am not understanding why it should add 0xFFFFFF in its previous byte.

I am using Keil IDE-Version: µVision3 V3.60 and C Compiler: C51.Exe V8.12

Can anyone tell me, if something is wrong ?

regards,
Kume

Parents
  • When a signed number is converted to more bits, the value is sign-extended. I.e. if most significant bit is set, it tells that the number is negative. When storing in a larger format, then all the extra bits must also be one to still represent the same negative value.

    0xa0 have the most significant bit set. So if stored in a signed byte, it doesn't represent the value 160. And if moved to a 16-bit signed integer, the value stored will be 0xffa0. If stored in a 32-bit integer, the value stored will be 0xffffffa0.

    So signed or unsigned is really very important.

    Another thing. You continue to use long constants without considering using the ul suffix. Why?

Reply
  • When a signed number is converted to more bits, the value is sign-extended. I.e. if most significant bit is set, it tells that the number is negative. When storing in a larger format, then all the extra bits must also be one to still represent the same negative value.

    0xa0 have the most significant bit set. So if stored in a signed byte, it doesn't represent the value 160. And if moved to a 16-bit signed integer, the value stored will be 0xffa0. If stored in a 32-bit integer, the value stored will be 0xffffffa0.

    So signed or unsigned is really very important.

    Another thing. You continue to use long constants without considering using the ul suffix. Why?

Children
No data