Yahoo Groups archive

AVR-Chat

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

Thread

logical shift operation

logical shift operation

2009-06-26 by Антощенков Роман Викторович

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!

  Same in dissasemble view:
  
289:        i1 = 1024;
+00000450:   E080        LDI       R24,0x00       Load immediate
+00000451:   E094        LDI       R25,0x04       Load immediate
+00000452:   839D        STD       Y+5,R25        Store indirect with displacement
+00000453:   838C        STD       Y+4,R24        Store indirect with displacement
290:        i2 = i1 >> 2;
+00000454:   818C        LDD       R24,Y+4        Load indirect with displacement
+00000455:   819D        LDD       R25,Y+5        Load indirect with displacement
+00000456:   9596        LSR       R25            Logical shift right
+00000457:   9587        ROR       R24            Rotate right through carry
+00000458:   9596        LSR       R25            Logical shift right
+00000459:   9587        ROR       R24            Rotate right through carry
+0000045A:   839B        STD       Y+3,R25        Store indirect with displacement
+0000045B:   838A        STD       Y+2,R24        Store indirect with displacement
291:        c1 = (unsigned char)i1>>2;

Why register R24? Must be R25

+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




-- 
Best regards,
Roman Antoshchenkov
mailto:djantoxa@rambler.ru

Re: logical shift operation

2009-06-27 by vietdung79

>
> 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!
>
> --
> Best regards,
> Roman Antoshchenkov
> mailto:djantoxa@...
>
c1 is a uchar so it can't be 256 (0x100). It is casted to 0.
If you want c1=256, you have to declare c1 as unsigned int.

Regards.

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.

Re[2]: [AVR-Chat] logical shift operation

2009-06-27 by Антощенков Роман Викторович

Hello!

Problem solved. It is my fault.

>> i1 = 1024;
>> i2 = i1 >> 2;
>> c1 = (unsigned char)i1>>2;
>>
>> Why c1 = 0? Must be c1 = 256!

 i1 = 1023; !
 i2 = i1 >> 2;
 c1 = (unsigned char)i1>>2;
 c1 = 255 !
-- 
Best regards,
Roman Antoshchenkov
mailto:djantoxa@rambler.ru

Re: logical shift operation

2009-06-27 by stevech11

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

I make a habit of coding as below to make operator precedence explicit:
 c1 = (unsigned char)(i1>>2);


--- In AVR-Chat@yahoogroups.com, Антощенков Роман Ð'икторович <djantoxa@...> wrote:
Show quoted textHide quoted text
>
> Hello!
> 
> Problem solved. It is my fault.
> 
> >> i1 = 1024;
> >> i2 = i1 >> 2;
> >> c1 = (unsigned char)i1>>2;
> >>
> >> Why c1 = 0? Must be c1 = 256!
> 
>  i1 = 1023; !
>  i2 = i1 >> 2;
>  c1 = (unsigned char)i1>>2;
>  c1 = 255 !
> -- 
> Best regards,
> Roman Antoshchenkov
> mailto:djantoxa@...
>

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.