[Icc-avr] Re: Casting (Morten Dramstad)

Morten Dramstad Morten at biocontrol.no
Mon Jan 14 13:59:16 PST 2008


Hi!
I always cast like this:



*(int *)&data[7] = channelOne;

data[7] will then be equal to the LSB and data[8] the MSB ofchannelOne.

The same for a long variable:

char data[10];
long longVar;

*(long *)&data[4] = longVar;

data[4] will be set equal to the LSB of longVar
data[5] will be set equal to the second LSB of longVar
data[6] will be set equal to the second MSB of longVar
data[7] will be set equal to the MSB of longVar

This will compilate into the shortest possible assembly code(is my
experience using different compilers, including Imagecraft ICCAVR).

Morten Dramstad

-------------------------------


Date: Sat, 12 Jan 2008 10:40:04 -0000
From: "Andrew" <andrew_166 at msn.com>
Subject: [Icc-avr] Casting
To: "Discussion list for ICCAVR and ICCtiny Users. You do NOT need
	tosubscribeto icc-announce if you are a member of this."
	<icc-avr at imagecraft.com>
Message-ID: <BLU123-DAV71A63AD833FEEBAF6CA5BA0440 at phx.gbl>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
	reply-type=response

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);

Or am i best doing: -

Data[7] = ((unsinged char)Channel_One >> 8);
Data[8] = ((unsinged Char)Channel_One);

or

Data[7] = (((unsigned char)(Channel_One & 0xF0)) >> 8 );
Data[8] = (Channel_One & 0x0F);



Andy





More information about the Icc-avr mailing list