Question

Using java, I am trying to recreate an arithmetic logic unit which performs an integer multiplication....

Using java, I am trying to recreate an arithmetic logic unit which performs an integer multiplication. I should able to get an output like this:

387637653 x 366499587 = 142069039730149311

But I am not sure because something is wrong apparently.

The algorithm of the program goes like:

  1. Test multiplier0
    1. If multiplier0 = 0
      1. Shift the multiplicand register left 1 bit
      2. Shift the multiplier register right 1 bit
    2. If multiplier0 = 1
      1. Add multiplicand to product and place the result in product register
      2. Shift the multiplicand register left 1 bit
      3. Shift the multiplier register right 1 bit
  2. Do the above 32 times
  3. End

import java.util.*;

public class HWMult {

       public static void main(String[] args)

       {

              Scanner x = new Scanner(System.in);

              System.out.println("Please enter your multiplier: ");

              int multiplier0 = x.nextInt();

              System.out.println("Please enter your multiplicant: ");

              long multiplicant = x.nextLong();

              long product = 0;

              int rep = 0;

             

              while(rep<=32)

              {

                     if (rep ==0 || multiplier0 == 1)

                     {

                           product = multiplicant + product;

                           multiplicant = multiplicant << 1;

                           multiplier0 = multiplier0 >> 1;

                     }

                    

                     else if(multiplier0 == 0)

                     {

                           multiplicant = multiplicant << 1;

                           multiplier0 = multiplier0 >> 1;

                     }

                          

                     rep++;

              }

             

              System.out.println("your product:"+ product);

       }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

You were checking always for multiplier only but since this is in binary, we need to check for the last digit whether it is divisible by 2 or not using multiplier0%2.

Caution: Please ensure that integer and long overflow didn’t happen. Please take care of the range.

The input given by you:

387637653 x 366499587 = 142069039730149311

works fine now.

Code:

import java.util.*;

public class Main

{

              public static void main(String[] args)

       {

              // scanner to take input from the user

              Scanner x = new Scanner(System.in);

              System.out.println("Please enter your multiplier: ");

             

              // take multiplier input from user

              int multiplier0 = x.nextInt();

             

              System.out.println("Please enter your multiplicant: ");

             

              // take multiplicand input from user

              long multiplicant = x.nextLong();

             

              // initialize the variables

              long product = 0;

              int rep = 0;

             

              // loop for 32 times and

              // repeat the process mentioned in the algorithm

              while(rep<=32)

              {

                     // if the last bit is 1

                     if (multiplier0%2 == 1)

                     {

                           // add product to multiplicant

                           // store it back in product

                           product = multiplicant + product;

                          

                           // left shift multiplicant by 1 bit

                           multiplicant = multiplicant << 1;

                          

                           // right shift multiplier0 by 1 bit

                           multiplier0 = multiplier0 >> 1;

                     }

                     // if the last bit is 0

                     else if(multiplier0%2 == 0)

                     {

                        // left shift multiplicant by 1 bit

                        multiplicant = multiplicant << 1;

                       

                        // right shift multiplier0 by 1 bit

                        multiplier0 = multiplier0 >> 1;

                     }

                    

                     // increment the counter

                     rep++;

              }

              System.out.println("your product:"+ product);

       }

}

Screenshot of the output:

Add a comment
Know the answer?
Add Answer to:
Using java, I am trying to recreate an arithmetic logic unit which performs an integer multiplication....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Multipliero = 1 Multipliero = 0 Multiplicand Shift Left 1. Test Multiplero 64 bits Multiplier Shift...

    Multipliero = 1 Multipliero = 0 Multiplicand Shift Left 1. Test Multiplero 64 bits Multiplier Shift Right la. Add multiplicand to product and place the result in Product register 64-bit ALU 32 bits Product Write 64 bits Control Test 2. Shift the Multiplicand register left I bit 3. Shift the Multiplier register right I bit 324 Repetition Yes Done b) [20 Points] In class we discussed the implementation of the multiplication of 32- binary numbers. The above figure shows the...

  • implement the following hardware synchronously in verilog سخت افزار ضرب Start Multiplier0 = 1 Multipiero =...

    implement the following hardware synchronously in verilog سخت افزار ضرب Start Multiplier0 = 1 Multipiero = 0 1. Test Multipliero 1a. Add multiplicand to product and place the result in Product register 2 Shift the Multiplicand register left 1 bit 3. Shift the Multiplier register right 1 bit No: < 32 repetitions 32nd repetition? Multiplicand Shift left B4 bits Yes: 32 repetitions 64-bit ALU Done Nultiplier Shit right 32 bits Product Controltos! Write 64 bits

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Java Branches code not working please HELP!! I am trying to build this class that will...

    Java Branches code not working please HELP!! I am trying to build this class that will ask for an ingredient, and the number of cups of the ingredient. It should then branch into an if/else branch off to verify that the number entered is valid and that it is within range. Once that branch completes it should then continue on to ask for the calories per cup, and then calculate total calories. Once I get it to enter the branch...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • 4) This exercise will first present the modified algorithm for computing the product of two numbers...

    4) This exercise will first present the modified algorithm for computing the product of two numbers represented in twos complement with an illustrated example and then ask you to repeat for a different number pair The hardware and the flowchart for signed multiplication in twos complement representation of binary numbers will be slightly modified as follows. Use the version of the unsigned multiplication hardware which employs one double-sized register to hold the partial product and the multiplier a. When shifting...

  • I am trying to understand how to manipulate the Gregorian Calendar so that I can convert...

    I am trying to understand how to manipulate the Gregorian Calendar so that I can convert my answer into a string format. What I'm hoping to do is to do is return string to a different file to add to a string in separate file (Object-oriented exercise between files), but once I can convert this into a string I can convert this file on my own. I added the System.out.println() to show what the results should look like. In other...

  • I have spent so much time on this and I am having trouble getting all the...

    I have spent so much time on this and I am having trouble getting all the methods to work together correctly to run the code right, and I am not sure what is wrong with it. /* You will recreate one or two versions of the logic game shown in the Algorithms movie and you will ask which one the user would like to play. Upon completion of the game, display who won and ask if they would like to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT