DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 08-25-2004, 03:05 PM   #1
john
Member
 
Join Date: Jan 2003
Posts: 85
Default

Code:
/**********************************************************************/ /* By Jos A. Horsmeier /* © 1998 Jos A. Horsmeier /**********************************************************************/ #include <stdio.h> /* add two numbers without using the '+' operator */ unsigned int add(unsigned int a, unsigned int b) { unsigned int c= 0; unsigned int r= 0; unsigned int t= ~0; for (t= ~0; t; t>>= 1) { r<<= 1; r|= (a^b^c)&1; c= ((a|b)&c|a&b)&1; a>>= 1; b>>= 1; } for (t= ~0, c= ~t; t; t>>= 1) { c<<= 1; c|= r&1; r>>= 1; } return c; } /* multiply two numbers without using the '*' operator */ unsigned int mul(unsigned int a, unsigned int b) { unsigned int r; for (r= 0; a; b <<= 1, a >>= 1) if (a&1) r = add(r, b); return r; }
john is offline   Reply With Quote
Old 08-25-2004, 03:09 PM   #2
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default

useful ?
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline   Reply With Quote
Old 08-25-2004, 03:17 PM   #3
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

looks like the same i invented on my own once.

useful? indeed. for learning how that thing works, and for implementing it in hw.. at least an adder will possibly be my next step in school, horray!
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 08-25-2004, 03:33 PM   #4
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default

we had all kinds of adders last term in hardware
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline   Reply With Quote
Old 08-16-2006, 09:06 AM   #5
manixrock
New Member
 
Join Date: Aug 2006
Location: Romania
Posts: 1
Default Re: Multiplying/Adding 2 numbers without '*'/'+'

here's my take on the Sum function:

Code:
private int Sum(int a, int b) { int x = a ^ b; while ((a & b) != 0) { b = (a & b) << 1; a = x; x = a ^ b; } return x; } private uint Product(uint a, uint b) { uint p = 0; while (b != 0) { if ((b & 1) == 1) p = Sum(p, a); a <<= 1; b >>= 1; } return p; }

smaller huh? :P

Last edited by Reedbeta : 08-16-2006 at 11:21 AM. Reason: code tags, please!!
manixrock is offline   Reply With Quote
Old 08-16-2006, 11:59 AM   #6
roel
Senior Member
 
roel's Avatar
 
Join Date: Sep 2005
Location: .nl
Posts: 504
Default Re: Multiplying/Adding 2 numbers without '*'/'+'

Okay, this doesn't sound really friendly, but it was the first thing that came in my mind:

WHO ** CARES?

(Also notice that this reply is carefully crafted: it doesn't contain the letter x. Okay, that is a paradox. Oh crap, another one. Well, it doesn't contain a q either.)
roel is offline   Reply With Quote
Old 08-16-2006, 04:41 PM   #7
pater
Valued Member
 
Join Date: Oct 2005
Location: Switzerland
Posts: 117
Default Re: Multiplying/Adding 2 numbers without '*'/'+'

Well, I can imagine that not using the multiply operator does indeed make sense on some CPU's. I've got an old 8086 assembly book which states that using the above method for multiplication using a (normal) add command was faster than using the mul operation. Recent PC CPU's do not have this behaviour any more, a Pentium type CPU needs some 5 or 6 cycles for a mul (where the 8086 used around 100). But this rule could still apply for some embedded CPU's.
pater is offline   Reply With Quote
Old 08-17-2006, 01:55 AM   #8
roel
Senior Member
 
roel's Avatar
 
Join Date: Sep 2005
Location: .nl
Posts: 504
Default Re: Multiplying/Adding 2 numbers without '*'/'+'

Really? The Product function multiplies exactly like one does when multiplying on paper, only now in binary. How can a hardware mul be slower than that? Okay, this code has probably a better best-case execution time while the hardware mul has probably a best-case = worst-case time, but even then... the sum function is still useless, I guess, even 20 years ago.
roel is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 11:16 PM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.