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

Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am