Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] logical shift operation

2009-06-27 by David Kelly

On Jun 26, 2009, at 12:10 PM, Антощенков Роман  
Викторович wrote:

> Hello!
>
> I am coding in AVRStudio 4.16 and WinAVR-20090313
>
> Code:
>  unsigned int  i1 = 0;
>  unsigned int  i2 = 0;
>  unsigned char c1 = 0;
>
>  i1 = 1024;
>  i2 = i1 >> 2;
>  c1 = (unsigned char)i1>>2;
>
>  Why c1 = 0? Must be c1 = 256!

[...]

> 291:        c1 = (unsigned char)i1>>2;
>
> Why register R24? Must be R25

No, R24 is correct. You only ask for the least significant 8 bits of i1.

> +0000045C:   818C        LDD       R24,Y+4        Load indirect with  
> displacement
> +0000045D:   9586        LSR       R24            Logical shift right
> +0000045E:   9586        LSR       R24            Logical shift right
> +0000045F:   8389        STD       Y+1,R24        Store indirect  
> with displacement

No, the above generated code is doing exactly what you told it to. You  
cast i1 to (unsigned char) so the compiler dutifully picked up only  
the low 8 bits which were zero.

The following is not the same thing as what you wrote above, but  
perhaps what you intended?

c1 = (unsigned char)(i1>>2);

i1 equals 256. After the above c1 equals 64, but after your code c1  
equals 0.

--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.