User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,041 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,932 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 552 | Replies: 2
Reply
Join Date: Jun 2008
Location: WA, USA
Posts: 681
Reputation: Alex Edwards has a spectacular aura about Alex Edwards has a spectacular aura about Alex Edwards has a spectacular aura about 
Rep Power: 4
Solved Threads: 56
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Practically a Master Poster

How do you divide without using division/multiplication?

  #1  
Jun 21st, 2008
I found an interesting question on the internet...

An interviewer asked a programmer "How would you divide without using division or multiplication?" And the programmer came up with some weird bit shifting operation that did the division.

Now, I'd like to make a program that does something similar, since--

int a = 10;
a = a << 3

means 10 * (2^3)

and

a = 10
a = a >> 3

means 10 / (2^3)

--but I'd like to redefine the right operation (the 2 to the power of (arg) ) with an object from the bitset<N> class so that when a user does something like this..

bitset<4> bSet;
int a = 10;

for(int i = 0; i < 3; i++)
bSet.set(i); //should set the first 3 bits to 1, for 0111 or 2^2 + 2^1 + 2^0 = 7

a = a >> bSet;

... and perform 10 / 7 division.

I have an idea of how I should do this...

//somewhere in the operator overload command--

return arg1 / pow(2, log(arg2.to_ulong())/log(2));

--the question, though it may seem trivial, is how do I make this operator function overload the natural bitshifting operation for these operations? In short, where is the bitshifting defined exactly? I heard that << and >> respectively were cout and cin objects operators, but if they can be used for pure bitshifting then I doubt they'd be restricted to those classes.
Last edited by Alex Edwards : Jun 21st, 2008 at 2:46 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2006
Posts: 263
Reputation: Colin Mac is on a distinguished road 
Rep Power: 2
Solved Threads: 16
Colin Mac Colin Mac is offline Offline
Posting Whiz in Training

Re: How do you divide without using division/multiplication?

  #2  
Jun 21st, 2008
I found an interesting question on the internet...
An interviewer asked a programmer "How would you divide without using division or multiplication?"
Division is repeated subtraction.
6/3 = 2.
3 can be subtracted from 6 twice, until you're left with zero. Which explains why dividing by 0 is infinity.
Multiplication is simply repeated addition.
Some old CPU's had no instructions for division and multiplication so people had to multiply/divide that way.
Last edited by Colin Mac : Jun 21st, 2008 at 6:33 pm.
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 681
Reputation: Alex Edwards has a spectacular aura about Alex Edwards has a spectacular aura about Alex Edwards has a spectacular aura about 
Rep Power: 4
Solved Threads: 56
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Practically a Master Poster

Re: How do you divide without using division/multiplication?

  #3  
Jun 21st, 2008
Originally Posted by Colin Mac View Post
Division is repeated subtraction.
6/3 = 2.
3 can be subtracted from 6 twice, until you're left with zero. Which explains why dividing by 0 is infinity.
Multiplication is simply repeated addition.
Some old CPU's had no instructions for division and multiplication so people had to multiply/divide that way.


That's genius =)

I'll definitely have to remember that XD
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC