Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Division without using divide operator

 
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?
 
(instanceof Sidekick)
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 ]
 
Rancher
Posts: 1197
22
  • 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).
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic