|
|
View previous topic :: View next topic |
Author |
Message |
young
Joined: 24 Jun 2004 Posts: 285
|
how div_t and ldiv_t are defined |
Posted: Mon Jul 12, 2004 6:33 am |
|
|
I need to get both the quotient and remainder of two long integert division in order to control system flow. does this work? I did not know what is the type ldiv_t, div_t is defined in ccs.
long quotient, remainer, result
result=ldiv(lnum,ldenom);
quotient=result.ldenom;
remainder=result.rem;
like
result=ldiv(1200000000,65536);
quotient=result.ldenom;
remainder=result.rem |
|
|
Ttelmah Guest
|
Re: how div_t and ldiv_t are defined |
Posted: Mon Jul 12, 2004 6:57 am |
|
|
young wrote: | I need to get both the quotient and remainder of two long integert division in order to control system flow. does this work? I did not know what is the type ldiv_t, div_t is defined in ccs.
long quotient, remainer, result
result=ldiv(lnum,ldenom);
quotient=result.ldenom;
remainder=result.rem;
like
result=ldiv(1200000000,65536);
quotient=result.ldenom;
remainder=result.rem |
ldiv_t is defined in stdlib.h.
What you show won't work, since a 'long' in CCS, is only a 16bit integer. It also won't work, because you can't use the 'dot' notation without defining the structure.
If you only need to divide by 65536, then remember that given a 'long' (or more importantly an int32, since your arithmetic would need it), is unsigned, you can simply code as:
union {
int32 lword;
int16 word[2];
} value;
value.lword=1200000000L;
Then value.word[0] is this /65536, and value.word[1] is the remainder.
Best Wishes |
|
|
Guest
|
|
Posted: Mon Jul 12, 2004 1:12 pm |
|
|
Thank you:
you have really sharp eyes. yes, as you pointed out, I realized late also that what I given as an example is not correct. since in ldiv (lnum,ldenom), lnum is long, not int32, ldenom is long; however, I still could not clearly understand the sample you are providing, could you provide a more detail int32 divide function? |
|
|
Ttelmah Guest
|
|
Posted: Mon Jul 12, 2004 2:04 pm |
|
|
Anonymous wrote: | Thank you:
you have really sharp eyes. yes, as you pointed out, I realized late also that what I given as an example is not correct. since in ldiv (lnum,ldenom), lnum is long, not int32, ldenom is long; however, I still could not clearly understand the sample you are providing, could you provide a more detail int32 divide function? |
I'm not using a divide function.
If you want to divide by 65536, then you only need the top 16bits of the value, and the remainder is the low 16bits of the same value. The union, says that the 'lword' value, is stored into the same memory locations, as the two 16bit array elements. You can just store a number into the 32bit location, and access the two parts as seperate variables. it is faster than any form of multiplication or division.
Best Wishes |
|
|
|
|
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
|