Discussion in "Software" started by    nismo    Aug 15, 2008.
Fri Aug 15 2008, 06:42 pm
#1
What >> and << do in C programming ??
Fri Aug 15 2008, 10:44 pm
#2
less then greater than
Sat Aug 16 2008, 04:43 pm
#3


less then greater than

Ûž TPS Ûž




Are you sure that it is greater or less than?? According to my textbook. >> means shift right and << shift left. But i don know where to apply both of them and how are are used...
Sat Aug 16 2008, 07:07 pm
#4
yes nismo,
u are right. these are indeed left/right shift operators. The most general use of these operators in our embedded c programming is to set/clear an individual bit of a single byte.
the left shift operator << is used in this case.
suppose u want to set the 'n' th bit of a register say reg1. then u can write the following instruction:
reg1 |=(1<<n);
similarly if u want to reset/clear a bit of the register u can use the instruction:
reg1 &=~(1<<n);
this logic is extensively used in AVR programming as AVRs have only byte level operations unlike 8051 which supports bit level manipulations .
the AVR library also included a macro _BV(n) which is equivalent to (1<<n) for this very reason.

Sun Aug 17 2008, 10:40 am
#5
pdi33 is right

x>>1
means the value of x be shifted one times towards right
like in
0000 0010 >>1 will give 0000 0001
similarly,


x<<1
means the value of x be shifted one times towards left
like in
0000 0010 <<1 will give 0000 0100


in some micro controllers bit addressing is not provided (ex. lpc2138)
there the whole (8 bit /32 bit) register needs to be read or written to.
in such a case you will need these (<<,>>) operators also known as bitwise shift operators.

ex. writing 1 to 5 bit of resister A
A = 0x01 << 5 ;
that will make A= 0010 0000 = 0x20
similarly if you want to read the status of the 5th bit of A register
(A>>5)&&(0x01) = result;

if the fifth bit is one u get result as 1
if not the result will be a zero..

hope that will make there use clearer!
:-)

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

RolandPeamb
Sun Oct 13 2024, 12:58 pm
MarcusLip
Sun Oct 13 2024, 01:17 am
AlfredTus
Sat Oct 12 2024, 10:38 pm
Mazrdoh
Sat Oct 12 2024, 09:25 am
BillyThems
Sat Oct 12 2024, 07:04 am
ctaletpnyi
Sat Oct 12 2024, 04:40 am
Sazrjbp
Fri Oct 11 2024, 09:40 pm
Sazrblh
Fri Oct 11 2024, 08:08 pm