CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Shift << or >> question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

Shift << or >> question
PostPosted: Mon Jul 19, 2004 5:19 pm     Reply with quote

I believe that when you do a shift left or right, << or >>, that zero's will always be inserted into the bits that are emptied. For example, if I do this:

variable = 10100111

then

variable = variable << 4;

the result is 01110000.

Is this what you have experienced? I just want to make sure before I try and trust things to work this way.

Ronald
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Mon Jul 19, 2004 6:13 pm     Reply with quote

Correct. Zeros will be shifted in.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 20, 2004 1:03 am     Reply with quote

For the left shift << it is true that always zero's will be shifted in.

For the right shift >> it is not specified in the C standard and left to the compiler builder. For unsigned data you always want a zero to be shifted in, but for signed data you want to shift in the same bit as already present in the most significant position.

If you want to be sure about the >> operation use the shift_right() function in which you can specify the bit to be shifted in.

Update: Did a small test on the PIC18F458 and PCH3.187.
A zero is always shifted in for both << and >> independent of signed or unsigned data. The shift_right() function is just as efficient as the << or >> operator.
Code:
....................   int8 Temp; 
....................   unsigned char uTemp; 
....................   signed char sTemp; 
....................     
....................   uTemp = 0; 
*
00EE:  CLRF   19
....................   sTemp = -1; 
00F0:  MOVLW  FF
00F2:  MOVWF  1A
....................   uTemp = uTemp >> 1; 
00F4:  BCF    FD8.0
00F6:  RRCF   19,F
....................   shift_right(&uTemp, 1, 0); 
00F8:  BCF    FD8.0
00FA:  RRCF   19,F
....................   shift_right(&uTemp, 1, 1); 
00FC:  BSF    FD8.0
00FE:  RRCF   19,F
....................   sTemp = sTemp >> 1; 
0100:  BCF    FD8.0
0102:  RRCF   1A,F
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group