|
|
View previous topic :: View next topic |
Author |
Message |
GhostofK Guest
|
Left shift question |
Posted: Sun Jun 04, 2006 3:04 pm |
|
|
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: 1909
|
|
Posted: Sun Jun 04, 2006 3:13 pm |
|
|
After the shift operation mask will be 0x02 (binary 10). Shifts always shift in zeroes. |
|
|
Ttelmah Guest
|
|
Posted: Sun Jun 04, 2006 3:14 pm |
|
|
<<, 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
|
|
Posted: Sun Jun 04, 2006 3:45 pm |
|
|
Thanks for the help! |
|
|
GhostofK
Joined: 04 Jun 2006 Posts: 6
|
|
Posted: Sun Jun 04, 2006 4:52 pm |
|
|
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? |
|
|
|
|
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
|