•
•
•
•
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
![]() |
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.
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.
•
•
Join Date: Sep 2006
Posts: 263
Reputation:
Rep Power: 2
Solved Threads: 16
•
•
•
•
I found an interesting question on the internet...
An interviewer asked a programmer "How would you divide without using division or multiplication?"
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.
•
•
•
•
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Polynomial operators-addition,substraction,division,remainder etc (C++)
- Factorial of any length (C++)
- please..help me....!! (Java)
- Unsigned Division (Assembly)
- large number division (C++)
- Wierd error messages with calculator program (C++)
- compilers in C (C++)
- It's Alive!! Program with a mind of it's own.... (Computer Science and Software Design)
Other Threads in the C++ Forum
- Previous Thread: time to link forms
- Next Thread: 2D Array


Linear Mode