# Integer Arithmetic Rules!

Careful thought and planning will enable you to write compact programs that execute speedily and provide accurate results without the need for floating-point math.

I was discussing a project with a young colleague who was trying to make a measurement using an ADC (analog-to-digital converter) and convert this measurement into a voltage to be displayed. He was playing around with the floating-point functions of the compiler. I observed that there was no real need to use floating-point data or math operations. When coupled with a response to a previous blog of mine, this suggested to me that a brief recap on integer arithmetic would not go amiss.

Back in the day, when the 2 kbyte 8048 microcontroller was the latest and greatest thing on the market, and when you were hand-coding or using an assembler (if you were lucky), memory space and execution speed were very high on your list of budgetary limitations. If you had to perform any mathematical processing, you had to consider your approach very carefully.

Firstly, there were no standard libraries readily available. Intel did have one called "Insight" (or maybe "Insite"), but it was expensive to join and you were (or at least, I was) reduced to scouring application notes and design ideas to find that ideal 16x16 multiplication routine. Oftentimes you would have to write your own.

Today some of these really smart tricks appear in books like *Hacker's Delight* by Henry S. Warren, Jr. If you really want to be impressed, you should look at the work done by Jack Crenshaw in his book *Math Toolkit for Real-Time Programming*. Today the integer arithmetic functions are either included in the compiler or there are many resources available with the routines, so you don't have to go to the very basics, but you can still avail yourself of the early techniques.

Let me state here and now that I am using the term "integer arithmetic" to cover the mathematical concept of integer and not the C compiler *int* data-type declaration. As we will see, the data types will have to be selected to suit our ends.

Let's assume that you want to read a 12-bit ADC with a span of 5V. The voltage would be simply given by (N/(N_{max} - N_{min}))*V_{ref}, where N is the current reading, N_{max} is the reading at the maximum voltage, N_{min} is the reading at the minimum voltage, and V_{ref} is -- of course -- the 5V of the ADC reference.

As a starting point, we assume the full range of the ADC conversion; hence, N_{max} = 4,095 and N_{min} = 0. Thus the voltage is given by (N/4,095)*5. With a floating-point calculation, the order of calculation is not significant (at least at the first approximation), but you can still perform this calculation with integer arithmetic and get a respectable result, if you think about what you are doing. Let's assume the number N that we measure is 1,234, so -- using a calculator -- the result is 1.507V. However, if you performed this using integer math with the operations in the order they are written, the integer division 1,234/4,095 will return a zero (any remainder is "thrown away"), and any product involving one or more zeros will result in a zero.

However, if we choose to first multiply the numerator with V_{ref}, this will yield an interim result of (1,234*5) = 6,170. You should have been taught that the number of bits needed to represent a product in binary is the sum of the number of bits of each multiplicand. You need to make sure that the interim variable is correctly declared to handle the maximum number of bits in the result. If we now perform an integer divide by 4,095, we will obtain a result of 1. This is closer to the true answer, but definitely "no cigar."

Author

U. Dreher 3/20/2014 1:41:02 PM

Until it is sent to an output the "physical value" will only con-

sume memory and CPU power (to calculate) but the result does not hold more information than the mere 1234d value from the ADC.

Nowadays the fresh-bred engineers from univerity are calling for floating point units - seemingly not aware that integer arithmetics exist at all. About 15 years ago I was tasked with improvements on an automotive ECU - based on an i80196. 90 % of the software was in assembler (primarily due to memory restrictions) and there was absolutely no FP arithmetics.

I was fiddling with data resolutions of 1/4 rpm and - IIRC - 1/16 degree of camshaft angle -- and that was just fine. The only instance where the "computing numbers" were converted to "real world values" was in the visualizing tools (PC based). And it was THEIR damned job to calculate these values :)

Nowadays there are still some fields of application where you sould resort to integer arithmetics, e.g. EC control units. OK - CPUs with FPUs are readily available. But when it comes to total cost, power consumption etc. integer math is superior. And - BTW - easily more precise than 32 bit floating point.

One more: ever seen fixed-point arithmetics ?

Author

HankWalker 3/20/2014 11:50:30 AM

Author

prabhakar_deosthali 3/20/2014 1:30:27 AM

It is so fast compared to a floating point or the interger arithmetic , that I used it in one of motion control alogirithms . I was working on DEC LSI 11 processor based motion controller and the robotic 3 axis motion control required the interpolation of the robot arms path while it waas negotiating a complex chassi assembly.

The base2 arithemetic came very handy - time and memory effiecient and without much of the rounding errors.

Author

Max The Magnificent 3/19/2014 2:27:43 PM

Author

Max The Magnificent 3/19/2014 2:25:31 PM

the 8048 was 1Kx8. The 8049 was 2Kx8....Did you ever read the biographies of Bill Gates and Paul Allen -- the original Microsoft BASIC fit in 4K and that was with a floating-point library. I hadn't realized quite what an achievement it was until I read the biographies -- now I have a much better appreciation for the mannitude of the task.

Author

antedeluvian 3/19/2014 2:23:56 PM

not seen "Math Toolkit for Real-Time Programming" but the reviews on Amazon make it sound like it would be well-worth reading.Jack Crenshaw, author of the above book is a long time contributor to the magazine Embedded Systems Programming in his column "Programmer's Toolbox". It is a pleasure to read although he often goes way over my head.

Author

antedeluvian 3/19/2014 2:17:51 PM

Back in the day, when the 2 kbyte 8048 microcontroller was the latest and greatest thing on the market,Sorry, my mistake- the 8048 was 1Kx8. The 8049 was 2Kx8. National brought out a 4K version which they called the 8050. The EPROM version had a 28 pin piggy-back socket on the 40 pin DIP and you could install a regular EPROM in it, up to a 2732 (4Kx8)

Author

Max The Magnificent 3/19/2014 2:11:50 PM