# doubts

Sat Aug 16 2008, 04:43 pm

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

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.

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

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!

:-)

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!

:-)

*Powered by*

**e107 Forum System**