# Multiplication and shif operator

ather ali

Greenhorn

Posts: 15

posted 14 years ago

hi guys

can any one tell me how computer can multipy internally any number with 3,6,7,9 etc

cuz if u multiply any number with two that means

number << 1;

and

number << 2 // means multiplied by 4

number << 3 // means multipled by 8

how i can multipy a number with 3 5 7 etc

i hope u have understood my question..

can any one tell me how computer can multipy internally any number with 3,6,7,9 etc

cuz if u multiply any number with two that means

number << 1;

and

number << 2 // means multiplied by 4

number << 3 // means multipled by 8

how i can multipy a number with 3 5 7 etc

i hope u have understood my question..

Marilyn de Queiroz

Sheriff

Posts: 9059

12

posted 14 years ago

This question has nothing to do with Programmer Certification. I'm moving it to Java in General(intermediate).

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Rob Ross

Bartender

Posts: 2205

posted 14 years ago

In a modern binary computer, you can only use the integer value and multiply using decimal arthimetic, ie,

int i = 3;

i = i*3 ;// multiply by three

i = i * 5; //multiply by five

...etc

There's no bit-shifting trick for these. The "trick" only works for powers of two, because it's in Base-2.

Think about the same thing in the decimal system when multiplying by powers of 10...since we use base ten this is as simple as shifting over the digits and adding a zero on the end...

112 << 1 : multiplying by 10 "shifts" digits one time to left

(These are pretend - java doesn't have Base-10 shifting, but this is how it would look if it did )

112 << 1 = 1120 //multiplied by 10

13 << 2 = 1300 //mutliplied by 100

etc.

But the answer to your question is "you cant."

[ April 18, 2002: Message edited by: Rob Ross ]

int i = 3;

i = i*3 ;// multiply by three

i = i * 5; //multiply by five

...etc

There's no bit-shifting trick for these. The "trick" only works for powers of two, because it's in Base-2.

Think about the same thing in the decimal system when multiplying by powers of 10...since we use base ten this is as simple as shifting over the digits and adding a zero on the end...

112 << 1 : multiplying by 10 "shifts" digits one time to left

(These are pretend - java doesn't have Base-10 shifting, but this is how it would look if it did )

112 << 1 = 1120 //multiplied by 10

13 << 2 = 1300 //mutliplied by 100

etc.

But the answer to your question is "you cant."

[ April 18, 2002: Message edited by: Rob Ross ]

Rob

SCJP 1.4