Micom_Electric/마이컴_원칩 2013. 3. 24. 15:12

나누기 

x / 3 

x / 3 = (x * 85) / 256 = (x * 85) >> 8 --- 여기서 85/256 = 0.33 = x / 3

단, 자리 오버플로우 주의, 8비트 범위를 초과할 수 도 있다


x / 2 = x >> 1

x / 256 = x >> 8


Simple : n/100

x = (y * 10) >> 10;

Efficient inverse (1/x) for AVR

http://electronics.stackexchange.com/questions/4374/efficient-inverse-1-x-for-avr

There are some very good techniques mentioned in the book "Hackers Delight by Henry Warren and on his website hackersdelight.org. For a technique that works well with smaller microcontrollers when dividing by constants have a look at this file.


http://www.hackersdelight.org/divcMore.pdf


unsigned divu100(unsigned n) {

 unsigned q, r;

 q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) + (n >> 12) + (n >> 13) - (n >> 16);

 q = q + (q >> 20);

 q = q >> 6;

 r = n - q*100;

 return q + ((r + 28) >> 7);

}

FIGURE 10–12. Unsigned divide by 100.


기타 참고

On another note if you are trying to do a divide on a CPU that doesn't support divide there's a really cool way to do it in this Wiki article.

http://en.wikipedia.org/wiki/Multiplicative_inverse

posted by 털보네i
: