Jump to content
Sign in to follow this  
Havier

Bit Bug

Recommended Posts

BoostC 6.30

gives wrong result:

 

odd=(length&2);

0084 3002 MOVLW 0x02

0085 0570 ANDWF chksum_00000_arg_length, W

0086 00FC MOVWF CompTempVar65

0087 10EB BCF chksum_00000_1_odd,1

0088 187C BTFSC CompTempVar65,0

0089 14EB BSF chksum_00000_1_odd,1

 

i.e. odd=least significant bit of (length&2)

 

But, according to ansi c, odd should be

0, if (length&2)==0

1, if any other value (1-255)

Share this post


Link to post
Share on other sites

This is not a bug. Though you didn't specify the types of the variables it looks like 'odd' has a bit type. Because of this it gets it's value from the lovest bit of the expression result from the right. The behaviour you expect is for variables 8 bit or longer. If you declare 'odd' as 'char' you will get the behaviour as you described.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
OK, I see. I had expected the same behaviour as in "if (length&2) {}".

 

It is the same:) Maybe the confusion comes from your statement:

 

0, if (length&2)==0

1, if any other value (1-255)

 

which isn't correct :) It should look more like:

 

0, if (length&2)==0

not 0, if any other value (1-255)

 

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
This is not a bug. Though you didn't specify the types of the variables it looks like 'odd' has a bit type. Because of this it gets it's value from the lovest bit of the expression result from the right.

 

Bug or not depends on how you expect undocumented compiler features to work.

 

By experimentation I have determined (in BoostC) that bits are treated as 1 bit integers and bools are treated as, well, bools.

 

If the OP had used a bool instead of a bit he would have got his expected result along with an arguably dubious warning.

 

Personally I don't see much utility in 1 bit integers (are they signed or unsigned? lol) while I frequently (like the OP) want to assign a port or register bit as though it were a boolean.

 

The difference between bits and bools needs to be documented.

Share this post


Link to post
Share on other sites
The difference between bits and bools needs to be documented.

 

Yes this is a valid comment. I will add this to our todo list.

 

Regards,

Pavel

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.

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

Loading...
Sign in to follow this  

×
×
  • Create New...