Thread: Efficiently multiplies a number by a factor 2

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    23

    Efficiently multiplies a number by a factor 2

    I am relatively new to programming. I need help writing this program.

    Write a C-program that efficiently multiplies a number by a factor 2 to the power n. The number to multiply and n are variables, which get a value at the start of the program. Clue: 1 shift to the left is the same as multiplying by 2. 2 shifts to the left are the same as multiplying by 4. 3 shifts to the left are the same as multiplying by 8
    Last edited by Zach786; 05-01-2013 at 01:32 AM.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    [0][0][0][0] [0][0][0][0] thats your memory right now it equal 0.
    [0][0][0][0] [0][0][0][1] now it equal to 1
    [0][0][0][0] [0][0][1][0] now thats a 2
    [0][0][0][0] [0][1][0][0] this is a 4
    [0][0][0][0] [1][0][0][0] this is a 8
    [0][0][0][1] [0][0][0][0] this is a 16 notice how evertime the one move one bit to the left it just multiplies by 2?

    ok now
    [0][0][0][0] [0][0][1][1] thats a 3
    [0][0][0][0] [0][1][1][0] this is a 6

    [0][0][0][0] [1][1][1][1] fifteen
    [0][0][0][1] [1][1][1][0] thirty
    [0][1][1][1] [1][0][0][0] 120
    from right to left they increase by the power of two

    [2^7][2^6][2^5][2^4] [2^3] [2^2] [2^1][2^0]
    the bits can only be 1 or 0 but each space represents a number. 2 to the power of 0 is 1, 2 to the power of 1 is 2.

    hope this helps more than confuses.

    if you shift a bit off the end you lose that bit. but the basic int on most machines is
    [][][][] [][][][] [][][][] [][][][] this big i think.

    they are operators that will shift bits either to the right or left. shifting a bit to the right has the opposite effect, division by 2.
    Last edited by jamesedmonds; 05-01-2013 at 02:51 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Read this site's homework policy.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    It's ok, I didn't do his work, just tried to explain it the best I could. I have no intention of coding anything for this person, only trying an attempt at helping him understand the assignment a little better to be able to produce a bit of code to expand upon. Maybe he will one day return to help others if he manages to pass his classes . to the OP. GL with the rest of your assignment and if you produce some code please post it if you need more guidance.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by grumpy View Post
    Read this site's homework policy.
    Wasn't the "Clue" in the OP essentially explaining exactly what needs to be done anyway?

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Wouldn't the most efficient implementation use something like scalbn() or ldexp()? It isn't the answer the HW is looking for, but it isn't the student's fault if the teacher asks the wrong question...

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    Quote Originally Posted by KCfromNC View Post
    Wouldn't the most efficient implementation use something like scalbn() or ldexp()? It isn't the answer the HW is looking for, but it isn't the student's fault if the teacher asks the wrong question...
    I dont think a function call is faster than bit manipulation done on the fly. I dont think anything is faster than shifting bits with bitwise operators. Someone else who has more exp on it maybe could answer, but they is overhead with function calls not found with bitwise. <<have a nice day>>

  8. #8
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by jamesedmonds View Post
    [0][0][0][0] [0][0][0][0] thats your memory right now it equal 0.
    [0][0][0][0] [0][0][0][1] now it equal to 1
    [0][0][0][0] [0][0][1][0] now thats a 2
    [0][0][0][0] [0][1][0][0] this is a 4
    [0][0][0][0] [1][0][0][0] this is a 8
    [0][0][0][1] [0][0][0][0] this is a 16 notice how evertime the one move one bit to the left it just multiplies by 2?

    ok now
    [0][0][0][0] [0][0][1][1] thats a 3
    [0][0][0][0] [0][1][1][0] this is a 6

    [0][0][0][0] [1][1][1][1] fifteen
    [0][0][0][1] [1][1][1][0] thirty
    [0][1][1][1] [1][0][0][0] 120
    from right to left they increase by the power of two

    [2^7][2^6][2^5][2^4] [2^3] [2^2] [2^1][2^0]
    the bits can only be 1 or 0 but each space represents a number. 2 to the power of 0 is 1, 2 to the power of 1 is 2.

    hope this helps more than confuses.

    if you shift a bit off the end you lose that bit. but the basic int on most machines is
    [][][][] [][][][] [][][][] [][][][] this big i think.

    they are operators that will shift bits either to the right or left. shifting a bit to the right has the opposite effect, division by 2.
    I like how you laid out the binary, this is helpful for me to see it like this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with prime factor for 12 digit number
    By nick2 in forum C Programming
    Replies: 15
    Last Post: 06-19-2009, 04:39 AM
  2. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  3. last prime factor
    By frango9000 in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 12:42 PM
  4. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  5. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM