Embedded Programming Tips and Tricks

From EEVblog Electronics Resource Wiki
Revision as of 22:33, 12 November 2011 by 60.234.168.104 (talk) (Created page with "===Fast Division=== Very fast division can be done by bit shifting a variable right. Each shift does a divide by two. For example Y = Y >> 1 Will result in a divide by 2 X = X...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Fast Division

Very fast division can be done by bit shifting a variable right. Each shift does a divide by two. For example

Y = Y >> 1 Will result in a divide by 2

X = X >> 4 Will result in a divide by 16


By adding a multiplication you can achieve other, more complex, divisions.

For example

Z = Z * 3; Z = Z >> 4; (div 16)


Will result in a division of 5.33 (16/3 = 5.33)

Be sure to do the multiply first, otherwise bits will be lost. Also be sure the variable size is large enough to store the multiplied value.