Jump to content
Sign in to follow this  

No Carry Set When Adding Unsigned Ints

Recommended Posts

Bug description:

When i to add two unsigned integers like:


unsigned int i = 0xffff + 0xffff;


I expect the carry flag to be set, but is cleared.


Steps to reproduce:

Use the above statement anywhere in your program and inspect the carry flag in the status register.


Expected behaviour:

The carry flag should be set because the integers are unsigned.


Is the problem 100% reproduceable:



IDE version: 6.03

Compiler: BoostC

Compiler version: 6.03

Target device: PIC16F876A


Share this post

Link to post
Share on other sites

This is not a bug. C language doesn't know what is carry flag and doesn't have to behave like assembly.




Share this post

Link to post
Share on other sites

Also, the code shown is optimised out to say i = 0xFFFE :)


If you code it in separate statements:


	unsigned int x1 = 0xffff;
unsigned int x2 = 0xffff;
unsigned int x3 = x1 + x2;

then the carry flag will in fact be set correctly, but I certainly wouldn't put any dependency on it staying that way.


Out of interest, how would you normally code to catch this? In most C implementations you could use a check against the result because you have Long datatypes, but what would you do in BoostC?




Share this post

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...