Like Tree1Likes
  • 1 Post By quzah

Division without operators

This is a discussion on Division without operators within the C++ Programming forums, part of the General Programming Boards category; Please give hints to write program to divide 40 by 2 without using division operator....

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Division without operators

    Please give hints to write program to divide 40 by 2 without using division operator.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Study what the other operators do.
    C Operator Precedence Table
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Study the bitwise ones more carefully.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Just bear in mind that the equivalence of some bitwise operations with some mathematical operations is only guaranteed for unsigned integral types: don't rely on such equivalences for int, float, double ....
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jag_c View Post
    Please give hints to write program to divide 40 by 2 without using division operator.
    Overload the addition operator to divide.

    It's not crazy, it's C++!


    Quzah.
    whiteflags likes this.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    You might want to consider that log(a/b) == log(a) - log(b) in any base (for a and b both positive).

    No division on the right hand side of that expression .....
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shifting? division?
    By kiros88 in forum C Programming
    Replies: 3
    Last Post: 06-09-2010, 02:10 PM
  2. true division in C ?
    By acidblue in forum C Programming
    Replies: 3
    Last Post: 12-03-2009, 10:22 PM
  3. Long Division
    By oogabooga in forum C Programming
    Replies: 9
    Last Post: 01-10-2008, 10:36 AM
  4. Division by 0
    By ajaxthegreater in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-08-2005, 04:21 PM
  5. Array Division
    By Rvaren in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2002, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21