Hello experts, to avoid a division, I use integer arithmetics. The unsigned int x which is in the range of 2000 is first multiplied by a number s which is 2048 plus/minus 1000 and then shifted by 11 digits which should replace a division by 2048.
unsigned int near x; unsigned int s x = ( (unsigned long) x * (unsigned long) s ) >> 11 ;
Guenther, The line looked ok to me. I wonder if the fact that one variable is explicitly set as a near (and I presume the other is a far) is the problem. Perhaps making all variables far would help.
Mark and John, Thanks for a quite interesting discussion. I guess this just goes to show that coding preferences once set will probably never change. Once a path has been chosen, nobody likes to admit that it was the wrong one. My own view on this is to dumb down the code as much as humanly possible. I always use parentheses for everything. Since I occasionally work with other peoples code, I keep a printed list of operators and their precedence right above my monitor. I don't have the precedence memorized but I can find out in a second or two. Basically, I want to use C as a tool, I don't want to think about C... I want to think about the system I am implementing. I try to make my code as readable as possible. It is second only to functionality in importance to me. Here is an example of how I use parentheses:
/* Update the current station button pressed from hardware if a comms device doesn't have it pressed */ if ( (m_bStatButtonPressed == NO_STATION) || ( (m_bStatButtonPressed != NO_STATION) && (m_bStatButtonSource == i_bCommAddress()) ) && (m_bStatButtonSlavesUpdated >= i_bTotalCommSlaves()) ) { BYTE bIndex; BYTE bStatIndex;
Brad, you are 100% right!
Meanwhile, back at the ranch, did Guenter ever get his integer math to work?
yes, now finally it works. The compiler worked correctly, and the type casting does what I expected, but... one of the variables was at an address where there was no physical memory. After that became clear, the solution was easy. Thanks to everyone for your comments on my problem.