Question

In: Computer Science

Java program - you are not allowed to use arithmetic operations such as division (/), multiplication,...

Java program - you are not allowed to use arithmetic operations such as division (/), multiplication, or modulo (%) to extract the bits. In this exercise use only logic bit-wise operations. Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you CANNOT use the arithmetic division by 2 or the modulo operation to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out 1000010. Hints for Programming Exercise 3: Hint 1: The number n is already in binary inside the memory. All you need is to “extract” or “read” the bits CPSC 3303 Programming Project 1 individually. Read the next hints to know how. Hint 2: Consider the number n=66. In the memory, 66 is 0000000001000010. I can isolate the least significant bit (red rightmost bit) by using the logic operation AND (&). Compute n & 1 = 66 & 1. Try the operation x & 1 with x taking different values to find out the effect: the operation “& 1” returns the value of the least significant bit! Hint 3: Say, for example, you read the rightmost bit with the operation “ & 1”. How should you read the bit that is to the left of the least significant bit (i.e., the blue bit to the left of the red bit)? The hint is to push all the bits to the right after I extracted the rightmost bit. To push to the right, you can shift right (>>) the number n to the right. n >> 1 = 66 >> 1 = 0000000000100001: all bits are pushed to the right. Now, that bit became the rightmost bit. . . And you know how to read the rightmost bit.

import java.util.Scanner;

public class DecimalToBinary
{
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = scan.nextInt();
for (int i = 31; i >= 0; i--)
{
if ( ((1 << i) & n) != 0)
System.out.print(1);
else
System.out.print(0);
}
}
}

****This is what I have written so far, but the output below has way too many leading zeros. What do I need to do to fix this? ****

Enter a positive integer:
66
00000000000000000000000001000010

Solutions

Expert Solution

package tushar1;

import java.util.Scanner;

public class DecimalToBinary
{
   public static void main(String[]args)
   {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter a positive integer: ");
       int n = scan.nextInt();
       String s="";
       while(n>0)
       {
           if((n&1)==1)
               s="1"+s;
           else
               s="0"+s;
           n=n>>1;
       }
       System.out.println(s);
   }
}


Related Solutions

Java Program // DO NOT USE BREAK OR CONTINUE :) You are allowed to use the...
Java Program // DO NOT USE BREAK OR CONTINUE :) You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method CANNOT use break or continue moveXDownLeft takes a char and a two dimensional array of char as input and returns nothing: The method should find the first occurrence of the char in the array (searching from "top" to "bottom" and "left" to "right"), and...
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Create a web calculator with JavaScript. Only four operations (Add, Sub, Multiplication, and Division) Use a...
Create a web calculator with JavaScript. Only four operations (Add, Sub, Multiplication, and Division) Use a CE button to cancel the operation.    Use the following website or any online resource as a reference. Don’t use their CSS or any other code.                 https://freshman.tech/calculator/ I am trying to learn please make comments then I will understand better.
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form: <binary operand 1> <operator> <binary...
Programming Problem Design a program in java that can perform three different arithmetic operations based on...
Programming Problem Design a program in java that can perform three different arithmetic operations based on the user’s input. The three operations are 1. Summation of integers from 1 to m 2. Factorial of a given number n (n!) 3. Finding the leftmost digit of a given integer (note: you have to use int) This program should prompt the user a menu including four options, ask the user for one option, and perform the corresponding computation. This process will repeat...
Description: You are to develop a Java program that prints out the multiplication or addition table...
Description: You are to develop a Java program that prints out the multiplication or addition table given the users start and end range and type of table. This time several classes will be used. You are free to add more methods as you see fit – but all the methods listed below must be used in your solution. For the class Table the following methods are required: - Protected Constructor – stores the start and size of table, creates the...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT