JorgeF 0 Posted August 12, 2013 Report Share Posted August 12, 2013 (edited) Hi Using BoostC 7.20 for PIC18 and for PIC16 Test code compiled for the following targets PIC18F8722 / PIC16F1827 / PIC16F886 (sample bellow is for PIC18F8722) While doing some tests on the subject of another topic I found this. Looking at the code generated for the "cFilter >>=1;" statement it seems BoostC makes no difference on right shifting of a signed or an unsigned char. I expect to see sign propagation when shifting signed variables. C source with unsigned char { unsigned char cFilter = 0x80; for (char c=0; c<8; c++) { ToggleOutput(); cFilter >>= 1; } } Generated code (unsigned char right shift) { unsigned char cFilter = 0x80; 001A 0E80 MOVLW 0x80 001C 6E01 MOVWF main_2_cFilter for (char c=0; c<8; c++) 001E 6A02 CLRF main_3_c 0020 label1 0020 0E08 MOVLW 0x08 0022 6002 CPFSLT main_3_c 0024 D006 BRA label2 002E 2A02 INCF main_3_c, F 0030 D7F7 BRA label1 0032 label2 { ToggleOutput(); 0026 EC04F000 CALL ToggleOutp_00009 cFilter >>= 1; 002A 90D8 BCF STATUS,C 002C 3201 RRCF main_2_cFilter, F } } C source with signed char { char cFilter = 0x80; for (char c=0; c<8; c++) { ToggleOutput(); cFilter >>= 1; } } Generated code (signed char right shift) { char cFilter = 0x80; 0032 0E80 MOVLW 0x80 0034 6E01 MOVWF main_8_cFilter for (char c=0; c<8; c++) 0036 6A02 CLRF main_9_c 0038 label3 0038 0E08 MOVLW 0x08 003A 6002 CPFSLT main_9_c 0046 2A02 INCF main_9_c, F 0048 D7F7 BRA label3 { ToggleOutput(); 003E EC04F000 CALL ToggleOutp_00009 cFilter >>= 1; 0042 90D8 BCF STATUS,C 0044 3201 RRCF main_8_cFilter, F // ?? where is the sign propagation ?? } } EDIT: Some more testing showed that it only happens with "char" size variables, the code generated for "int" and "long" variables is fine. Best regards Jorge Edited August 12, 2013 by JorgeF Quote Link to post Share on other sites
Pavel 0 Posted August 13, 2013 Report Share Posted August 13, 2013 ...it seems BoostC makes no difference on right shifting of a signed or an unsigned char... BoostC does retain sign in signed shifts. In your test code though you use 'char' and 'unsigned char' which are same types. You need to replace 'char' with 'signed char'. Regards, Pavel Quote Link to post Share on other sites
JorgeF 0 Posted August 15, 2013 Author Report Share Posted August 15, 2013 Hi Thank you for the clarification Pavel. Don't know exactly why, maybe from some compiler I used long ago, I allways considered "char" as signed (just like an "int") but it seems that it can be signed or unsigned depending on compiler implementation. Best regards Jorge Quote Link to post Share on other sites
Reynard 0 Posted August 20, 2013 Report Share Posted August 20, 2013 A note in the manual to say that plain char is considered unsigned would help. Char should always be qualified with signed or unsigned if used for computation. Use plain char for text strings. In olden times char contained a printable character, which is always a positive value. Cheers Reynard 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.