Havier 0 Posted January 30, 2006 Report Share Posted January 30, 2006 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) Quote Link to post Share on other sites
Pavel 0 Posted January 30, 2006 Report Share Posted January 30, 2006 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 Quote Link to post Share on other sites
Havier 0 Posted January 30, 2006 Author Report Share Posted January 30, 2006 OK, I see. I had expected the same behaviour as in "if (length&2) {}". Quote Link to post Share on other sites
Pavel 0 Posted January 30, 2006 Report Share Posted January 30, 2006 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 Quote Link to post Share on other sites
harrt 0 Posted February 6, 2006 Report Share Posted February 6, 2006 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. <{POST_SNAPBACK}> 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. Quote Link to post Share on other sites
Pavel 0 Posted February 7, 2006 Report Share Posted February 7, 2006 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 Quote Link to post Share on other sites
Recommended Posts
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.