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 CCS Technical Support

Left shift question

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







Left shift question
PostPosted: Sun Jun 04, 2006 3:04 pm     Reply with quote

Hi, I'm working on a school project and I have another question about some code. I'm trying to do a left shift on a variable named 'mask'. If I write:
int16 mask = 1;
mask = mask << 1;
then will the result be mask = 10 or mask = 11?

The code that I'm using is right below as well.

void drive_led(int16 code){
int16 code_bit = 0x0000;
int16 mask = 0x0001;

delay_ms(20);
output_high(PIN_C1);
delay_ms(2400);
output_low(PIN_C1);
delay_ms(600);

while(mask != 0x1000){
code_bit = code && mask;
if(code_bit != mask){
output_high(PIN_C1);
delay_us(600);
output_low(PIN_C1);
delay_us(600);
}
else{
output_high(PIN_C1);
delay_us(1200);
output_low(PIN_C1);
delay_us(600);
}
mask = mask << 1;
}
}

Thanks for any help!
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Sun Jun 04, 2006 3:13 pm     Reply with quote

After the shift operation mask will be 0x02 (binary 10). Shifts always shift in zeroes.
Ttelmah
Guest







PostPosted: Sun Jun 04, 2006 3:14 pm     Reply with quote

<<, is equivalent to multiplying by two. So binary 001, becomes '010'.
If you want to shift a value in, then look at the 'shift left' command. This allows a value to be shifted in to fill the bottom bit. So:

Code:

int16 value=0b0000000000000001;

shift_left(&value,2,0);

//This gives 0b0000000000000010;

shift_left(&value,2,1);

//This gives 0b0000000000000101;

So with this command, you can elect to shift in a zero or a one as required.
Look also at:
Code:

int16 value=0b1000000000000000;

rotate_left(&value,2);

//This gives 0b0000000000000001;

This form, rotates, rather than shifting the word, in the example, taking the top bit, and rotating it back to the bottom bit.

Best Wishes
GhostofK
Guest







PostPosted: Sun Jun 04, 2006 3:45 pm     Reply with quote

Thanks for the help!
GhostofK



Joined: 04 Jun 2006
Posts: 6

View user's profile Send private message

PostPosted: Sun Jun 04, 2006 4:52 pm     Reply with quote

Hi, I'm having some trouble with my code that I posted above originally. For some reason, pin C1 is not going all the way high. The timing is perfect, but pin_C1 is only going up to about 600mV instead of the full 5v as it should. Does anyone know what could be the problem here?
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