Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 2, 2005 09:38:21

D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[Mspgcc-users] tip #7


cause of sign extension.
Every operation with signed variables will require a sign extension which
takes some time therefore slower.
~d

On Tuesday 01 November 2005 22:49, Kevin Schmeichel wrote:
> Does anyone know the reasoning behind tip # 7 from:
>
>http://mspgcc.sourceforge.net/manual/c1408.html>
> 7. Use int instead of char or unsigned char if you want an 8 bit integer.
>
> This is confusing to me because an int is supposedly 16 bits
> (http://mspgcc.sourceforge.net/manual/x987.html). Why is it not a good
> idea to use chars for small ints?
>
>
> Thanks,
> Kevin
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by:
> Tame your development challenges with Apache's Geronimo App Server.
> Download it for free - -and be entered to win a 42" plasma tv or your very
> own Sony(tm)PSP. Click here to play:http://sourceforge.net/geronimo.php> _______________________________________________
> Mspgcc-users mailing list
> Mspgcc-users@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Offline

#2 Nov. 2, 2005 16:52:40

Kevin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[Mspgcc-users] tip #7


On Wednesday 02 November 2005 1:38 am, Dmitry wrote:
> cause of sign extension.
> Every operation with signed variables will require a sign extension which
> takes some time therefore slower.
> ~d

Right, but tip #7 says:

7. Use int instead of char or unsigned char if you want an 8 bit integer.

So is it really better to use a (signed) int instead of an unsigned char?

Also, another question:

5. Avoid using global variables of small size - it is a waste of RAM.

Would I be correct in assuming that "small size" means a byte? And that the
waste would occur if a global byte variable was stored at an even address,
and the next global variable was a word variable, and had to be stored at the
next even address, so we'd lose a byte?

Kevin

Offline

#3 Nov. 8, 2005 07:31:16

Robert D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[Mspgcc-users] tip #7


Hello Kevin!

Kevin Schmeichel schrieb:Does anyone know the reasoning behind tip # 7 from:http://mspgcc.sourceforge.net/manual/c1408.html7. Use int instead of char or unsigned char if you want an 8 bit integer.This is confusing to me because an int is supposedly 16 bits(http://mspgcc.sourceforge.net/manual/x987.html). Why is it not a good ideato use chars for small ints?The reason is the C Standard. The arithmetic operations
aren't defined for char type variables. The compiler will
always convert chars to int types for arithmetic operations
and this may result in unexpected results.

For example:

char a;
char b;

a = 0xFF;
b = ~a;
if (b == ~a)
{
// This code might be not reachable because the implicite
// type conversion results in 0x0000 == 0xFF00
// which is always true
}

b = a + 1;
while (b != a + 1)
{
// This loop may never end, because the defined type
// conversion will result in the comparison b != 0x100
// which will be never true.
}

By this, for portability to different compilers and
code safety you need castings to char after each
mathematical operation or you should use an int type.





By the way: Is there a fix for the reported
bug in my email "Code generation error in msp430-gcc version 3.2.3"
(29th of July 2005) in the next version?

The code section was:


/* Calculation / Code generation problem */
/* calculated result is 63936 (-1600) instead of 24000 */

#define MACRO 240U

int Test (void)
{
~ extern unsigned short u16Result;
~ extern unsigned char u8Value;

~ u16Result = MACRO * u8Value;

~ return (int)u16Result;
}



Robert Dominicus-Schleutermann

--
Robert Dominicus-Schleutermann Telefon : +49 36203 96314
Software+Systeme Erfurt GmbH Telefon : +49 36203 96301
Fichtenweg 8 Fax : +49 36203 96333
D-99198 Erfurt-Kerspleben
eMail:mailto:robert.dominicus-schleuterm...@sse-erfurt.de

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support