Jump to content
JorgeF

Signed Char Right Shift

Recommended Posts

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 by JorgeF

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

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

×
×
  • Create New...