|
|
View previous topic :: View next topic |
Author |
Message |
Radomir
Joined: 18 Nov 2005 Posts: 3
|
data type conversion - assignments between different lenghts |
Posted: Fri Nov 18, 2005 2:46 pm |
|
|
Hello!
I have recently started using CCS compiler and I don't have much experience with C either.
I need to perform the following calculation:
var1*(var2/const), where:
var1 - 8bit integer
var2 - 8bit integer
const - 8 bit integer
1<var2<const
I assigned a 16bit temp var to hold var1*var2 and afterwards to divide it by const and assign it to the result var.
However, it seems that upper part of the temp var is lost.
Please, help! |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 18, 2005 3:51 pm |
|
|
The key is that the arithmetic used, is defined by the sizes of the operands, not the 'target'. Hence if you multiply an int8, by an int8, the compiler will use int8 arithmetic.
You can force 16bit arithmetic to be used in a number of ways:
1) If you declare 'const' to be a long (for instance 100L), then this will force a switch to using int16 arithmetic
2) If you change one of the values into an int16, before performing the arithmetic, this will also force the switch. This is the 'preferred' way to go, and is standard C.
The arithmetic rules for the conversion, are exactly as written in K&R, but with the 'caveat', that the default 'int' type (which for instance a char will be converted to), is int8, rather than int16, which makes the conversion 'seem' less friendly.
So, use:
var1*((int16)var2/const)
Which will force var2, to be converted to an int16, before the arithmetic is started. Then the division will be done using an int16, and since the result is now an int16, the multiplication will also be done using the larger type.
Best Wishes |
|
|
Radomir
Joined: 18 Nov 2005 Posts: 3
|
|
Posted: Mon Nov 21, 2005 9:38 am |
|
|
Ttelmah wrote: | The key is that the arithmetic used, is defined by the sizes of the operands, not the 'target'. Hence if you multiply an int8, by an int8, the compiler will use int8 arithmetic.
You can force 16bit arithmetic to be used in a number of ways:
1) If you declare 'const' to be a long (for instance 100L), then this will force a switch to using int16 arithmetic
2) If you change one of the values into an int16, before performing the arithmetic, this will also force the switch. This is the 'preferred' way to go, and is standard C.
The arithmetic rules for the conversion, are exactly as written in K&R, but with the 'caveat', that the default 'int' type (which for instance a char will be converted to), is int8, rather than int16, which makes the conversion 'seem' less friendly.
So, use:
var1*((int16)var2/const)
Which will force var2, to be converted to an int16, before the arithmetic is started. Then the division will be done using an int16, and since the result is now an int16, the multiplication will also be done using the larger type.
Best Wishes |
Thank you very much for the clue!
It looks like I have 16bit data now.
I also found another way of converting 8bit data to 16bit data - make16(v1,v2), and another way of multiplying 8bit vars - mul(v1,v2).
I am still not getting the right results, but I know the path.
Thanks again! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|