Multiplication of two numbers.

Discussion in 'Programming' started by webmaster8757, Dec 2, 2009.

  1. #1
    Hello , Actaully i am confused that how we can multiply two number without using any operators.? please help me giving yours ideas....
     
    webmaster8757, Dec 2, 2009 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Write a function

    function multiply($x,$y){
    return $x*$y;
    }

    echo(multiply(5,4)); // Output: 20
     
    s_ruben, Dec 2, 2009 IP
  3. webmaster8757

    webmaster8757 Member

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Hello dear thanks for Your reply but You have use * operator to call a functions.. my question is that witout using any operators how we can multiply two numbers....
     
    webmaster8757, Dec 2, 2009 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    Use
    function multiply($x,$y){
    $z = array();

    for($i=0; $i<$y; $i++){
    $z[$i] = $x;
    }

    return array_sum($z);
    }

    echo(multiply(5,4));

    and if you cannot use "for" statement write

    $x[0] = 5;
    $x[1] = 5;
    $x[2] = 5;
    $x[3] = 5;

    echo array_sum($x);
     
    s_ruben, Dec 2, 2009 IP
  5. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you referring to signed multiplication x86 ASM instruction which can be called from C++/Java as below?

    asm(imul dest, src1, imm_src);

    this does dest := src1 * imm_src
     
    NeoCambell, Dec 2, 2009 IP
  6. harishsyndrome

    harishsyndrome Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Without using any operator???

    I think there is a way in List where you need to Give

    multiply ( 2 3 )

    But not sure about that
     
    harishsyndrome, Dec 2, 2009 IP
  7. brian65

    brian65 Active Member

    Messages:
    1,172
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    88
    #7
    The question is a bit confusing - or at least difficult.

    Do you mean is there a quick way of multiplying a number by 2?

    In C or C++, you can do this by shifting, e.g. x = x << 1;
     
    brian65, Dec 4, 2009 IP
  8. webmaster8757

    webmaster8757 Member

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    Thanks for ur reply.dear can You explain me widely how can we use shift for multiplications...
     
    webmaster8757, Dec 5, 2009 IP
  9. webmaster8757

    webmaster8757 Member

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    #include<stdio.h>
    main()
    {
    int a,b,result;
    printf("nEnter the numbers to be multiplied :");
    scanf("%d%d",&a,&b);
    result=0;
    while(b != 0) // Iterate the loop till b==0
    {
    if (b&01) // Logical ANDing of the value of b with 01
    result=result+a; // Update the result with the new value of a.
    a<<=1; // Left shifting the value contained in 'a' by 1.
    b>>=1; // Right shifting the value contained in 'b' by 1.
    }
    printf("nResult:%d",result);
    }
     
    webmaster8757, Jan 10, 2010 IP
  10. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Note that you can only use shift operations for multiply/divide a number with another number if and only if one of them is a power of 2.

    That means if p is any number,

    p << 1 means p X (2^1), i.e.: p X 2
    p << 2 means p X (2^2), i.e.: p X 4
    p << 3 means p X (2^3), i.e.: p X 8
    p << 4 means p X (2^4), i.e.: p X 16
    p << 5 means p X (2^5), i.e.: p X 32
    ...
    ...
    ...
    p << n means p X (2^n)

    Similarly,

    p >> 1 means p / (2^1), i.e.: p / 2
    p >> 2 means p / (2^2), i.e.: p / 4
    p >> 3 means p / (2^3), i.e.: p / 8
    p >> 4 means p / (2^4), i.e.: p / 16
    p >> 5 means p / (2^5), i.e.: p / 32
    ...
    ...
    ...
    p >> n means p / (2^n)


    Now you can understand that if you need to multiply/divide p with q using shift operations, either p or q must be a number with power of 2.

    If you want to check whether a number is a power of 2, use following function.

    
    int IsPowerOfTwo(int x)
    {
        return (x != 0) && ((x & (x - 1)) == 0);
    }
    
    Code (markup):
     
    Last edited: Jan 10, 2010
    NeoCambell, Jan 10, 2010 IP
  11. cocodude

    cocodude Active Member

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #11
    Surely shift is an operator as well?
     
    cocodude, Jan 11, 2010 IP