Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Division without using divide operator

 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for the program/algorithm to implement division without
using divide operator?


I know the subtraction solution...I was looking for a more efficient solution ...something like bits manipulation of some kind?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The right shift operator will divide by powers of 2. That's the only shortcut I use. Here are a few notes on Binary Division which might be fun.

Is this a bar bet or a school assignment?
[ October 08, 2007: Message edited by: Stan James ]
 
Ryan McGuire
Ranch Hand
Posts: 1057
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implement binary long division. Sure, regular base 10 long division uses a divide operation, but since the binary equivalent needs to arrive at either a 0 or a 1, you could get away with an if statement.

This algorithm would be O(log product), while the subtraction algorithm would be O(result).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic