how to multiply two integers using bitwise operators

S

sandy_pt_in

How to mulitply two integer numbers using bitwise operators in C
language.Please reply as early as possible
 
A

Aggro

How to mulitply two integer numbers using bitwise operators in C
language.Please reply as early as possible

You do know how to sum them, right? Well check this out:

3*4 == 3+3+3+3

3*4 == 4+4+4
 
O

osmium

How to mulitply two integer numbers using bitwise operators in C
language.Please reply as early as possible

Emulate the way you learned to do it for decimal numbers in elementary
school.
 
N

Nick Austin

You do know how to sum them, right? Well check this out:

3*4 == 3+3+3+3

3*4 == 4+4+4

Well that's the easy bit. However + is not a bitwise operator.

Adding using only the bitwise operators is the hard bit of
this problem.

Nick.
 
S

Slartibartfast

How to mulitply two integer numbers using bitwise operators in C
language.Please reply as early as possible

Think about how you would do long multiplication in binary.
 
T

Thomas Matthews

How to mulitply two integer numbers using bitwise operators in C
language.Please reply as early as possible

Can the addition operator be used?

I was taught about left-shift and add, but adding is not a
bitwise operator.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
S

Slartibartfast

Thomas Matthews said:
but adding is not a bitwise operator.

....but it is easy to implement in terms of bitwise operators, as long as you remember the truth table for the full adder.
 
M

Micah Cowan

Slartibartfast said:
...but it is easy to implement in terms of bitwise operators, as long as you remember the truth table for the full adder.

....but the OP reeks of homework...
 
S

sandy_pt_in

Thomas Matthews said:
Can the addition operator be used?

I was taught about left-shift and add, but adding is not a
bitwise operator.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Condition was to use only bitwise operators. Eventhough can you tell
me that method you know, using add operation.I will scratch my head to
get somthing out of it.
 
L

LibraryUser

osmium said:
.... snip ...


A full adder has three inputs: addend, augend and carry in and
two outputs: sum and carry out. Carry in is 0 for the rightmost
bit. Make a truth table and note that the exclusive or is very
useful.

Start with a one-bit adder, where each input and output is a
single bit. Then cascade these to form multiple bit adders.
 
O

osmium

<I can't make sense of the attributions, so I snipped them. I suspect there
is one person using two identities.>
Condition was to use only bitwise operators. Eventhough can you tell
me that method you know, using add operation.I will scratch my head to
get somthing out of it.

A full adder has three inputs: addend, augend and carry in and two outputs:
sum and carry out. Carry in is 0 for the rightmost bit. Make a truth
table and note that the exclusive or is very useful.
 
M

Morris Dovey

Condition was to use only bitwise operators. Eventhough can you tell
me that method you know, using add operation.I will scratch my head to
get somthing out of it.

Addition can be viewed as a sequence of bitwise operations.
Exclusive or-ing the addend and augend yields all of the partial
sum bits; while and-ing the addend and augend yields all of the
carry bits. Shift the carry bits left one position.

Put the partial sum in the addend.

If there are no 1's in the carry bits, then you're done and the
result of the addition is in the addend.

Otherwise put the carry bits in the augend and repeat.

There are other ways to get the job done; but this is the easiest
to describe. 8^)

It's fairly easy to back into subtraction by negating and adding;
and a (very) simplistic multiplication can be implemented using
the addition algorithm to repeatedly add one of the factors to
the product and addition of -1 to the other factor.

Division can be done as a bitwise successive approximation; but
requires development of a bitwise magnitude comparison (left as
an exercise for the student :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top