SV: [Icc-avr] Casting
Steven Lose
sl at ecpower.dk
Sun Jan 13 23:20:15 PST 2008
Hi.
As Albert says, the resulting code is as expected.
I'm sure that ICC will do the right thing, but since you are asking, it must be because you are not sure.
When your colleagues is looking at it, some may also not be sure
So to convince yourself in a few month when you look at it again and you colleagues, that you knew exactly what you were doing, better write:
Data[7] = (unsigned char)(Channel_One >> 8);
Data[8] = (unsigned char)(Channel_One);
Or with even more explaining code:
Data[7] = (unsigned char)((Channel_One & 0xFF00) >> 8);
Data[8] = (unsigned char)(Channel_One & 0x00FF);
Or as Jörn Linden just wrote, use union if you don't share the source code on compileres from competing indian tribes:
Struct {
Unsigned int uiVar;
Unsigned char ucVar[2];
}uVar;
uVar.uiVar = Channel_One;
Data[7] = uVar.ucVar[1];
Data[8] = uVar.ucVar[0];
I haven't compared the output from the tree examples, so I don't know how big the difference is, but I expect the last one to be the fastest.
Med venlig hilsen / Best regards / mit freundlichen Grüßen
EC POWER A/S
Steven Lose
Software Ingeniør
Tlf.: +45 87434100
Direkte tlf. +45 58286608
Email: sl at ecpower.dk
www.ecpower.dk
-----Oprindelig meddelelse-----
Fra: icc-avr-bounces at imagecraft.com [mailto:icc-avr-bounces at imagecraft.com] På vegne af Albert vanVeen
Sendt: 13. januar 2008 20:17
Til: Discussion list for ICCAVR and ICCtiny Users. You do NOT needtosubscribeto icc-announce if you are a member of this.
Emne: RE: [Icc-avr] Casting
I've always used the first (simplest) solution, and an occasional look
at the assembly code looked perfectly alright, i.e. as expected.
Unless it is really necessary, casting doesn't help for readability,
does it?
But you might say: better safe than sorry.
In any case, a quick check of the final code shows you if there's any
danger.
Albert.
-----Original Message-----
From: icc-avr-bounces at imagecraft.com
[mailto:icc-avr-bounces at imagecraft.com] On Behalf Of John Baraclough
Sent: Sunday, January 13, 2008 04:39 AM
To: Discussion list for ICCAVR and ICCtiny Users. You do NOT need
tosubscribe to icc-announce if you are a member of this.
Subject: Re: [Icc-avr] Casting
Andrew wrote:
> Hi,
>
> When casting and int to two char' (a MSB and LSB) is it safe to do the
> following:-
> Data[7] = (Channel_One >> 8);
> Data[8] = (Channel_One);
>
No
> Or am i best doing: -
>
> Data[7] = ((unsinged char)Channel_One >> 8); Data[8] = ((unsinged
> Char)Channel_One);
>
Yes, but you need to adjust your parentheses and spelling.
> or
>
> Data[7] = (((unsigned char)(Channel_One & 0xF0)) >> 8 ); Data[8] =
> (Channel_One & 0x0F);
>
No, because Data[7] will always be 0 and Data[8] should be cast and will
have a maximum value of 15.
>
>
> Andy
>
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr
>
>
HTH
John
_______________________________________________
Icc-avr mailing list
Icc-avr at imagecraft.com
http://dragonsgate.net/mailman/listinfo/icc-avr
Scanned by Bizo Email Filter
_______________________________________________
Icc-avr mailing list
Icc-avr at imagecraft.com
http://dragonsgate.net/mailman/listinfo/icc-avr
More information about the Icc-avr
mailing list