Question

In: Computer Science

Background: Recall from the lecture notes that an integer x can be represented as a vector...

Background: Recall from the lecture notes that an integer x can be represented as a vector [xw−1, xw−2, . . . , x0],
where w is the number of bits used to represent x. As a fun exercise, let us consider a problem where we want to
partition x into two new numbers, labelled y and z. We can do this by scanning through the bits of x from the least
significant bit (lsb) to the most significant bit (msb). We form the new integers, y & z, according to the following
rule. Anytime a 1 is encountered during the scan through x we place a 1 in either y or z in an alternating pattern.
Moreover, the bits are placed into the new integers at the same bit position as found in the original integer x. Below
is an example.
Let x = (13)10 = (1101)2. Then, scanning from lsb to msb gives the following result.
• Position 0: x0 = 1. Thus, y0 = 1, giving y = (0001)2.
• Position 1: x1 = 0. Thus, nothing happens.
• Position 2: x2 = 1. Thus, z2 = 1, giving z = (0100)2.
• Position 3: x3 = 1. Thus, y3 = 1, giving y = (1001)2.
The final result is that y = (1001)2 = (9)10 and z = (0100)2 = (4)10.
Problem: Write a Java program that takes an integer x as an input and produces y and z as an output.
Sample Execution:
Enter an integer x
13
y z
9 4

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================


import java.util.Scanner;

public class Converter {

    public static int toDec(String binary) {


        int dec = 0;
        int power = 1;
        for (int i = binary.length() - 1; i >= 0; i--) {
            if (binary.charAt(i) == '1') dec += power;
            power *= 2;
        }
        return dec;
    }

    public static String toBin(int decimal) {

        String bin = "";
        while (decimal > 0) {
            bin = String.valueOf(decimal % 2) + bin;
            decimal /= 2;
        }
        return bin;
    }

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int x = scanner.nextInt();

        String binX = toBin(x);
        String y = "", z = "";
        boolean alternating = true;

        for (int i = binX.length() - 1; i >= 0; i--) {
            if (binX.charAt(i) == '0') {
                y = "0" + y;
                z = "0" + z;
            } else {
                if(alternating){
                    y = "1"+y;
                    z = "0" + z;
                    alternating = !alternating;
                }else{
                    y = "0"+y;
                    z = "1" + z;
                    alternating = !alternating;
                }
            }


        }
        System.out.println("y = "+ toDec(y));
        System.out.println("z = "+ toDec(z));

    }
}

=================================================================


Related Solutions

THIS IS ALL ON PYTHON Recursion Practice Recall from lecture that a recursive function is one...
THIS IS ALL ON PYTHON Recursion Practice Recall from lecture that a recursive function is one that calls on itself. Recursive functions have two parts, the base case which stops the recursion and the recursive step where the function calls upon itself. When designing recursive algorithms, your goal for each step should be to work towards the base case. More abstractly, each recursive call decreases the size of the problem, working ever closer to some trivial case with a defined...
Show that projective plane is homeomorphic to sphere modulo from vector x to - vector x
Show that projective plane is homeomorphic to sphere modulo from vector x to - vector x
Any integer can be represented in binary, octal, decimal, and hexadecimal. What are the limits on...
Any integer can be represented in binary, octal, decimal, and hexadecimal. What are the limits on representing integers using unary or only having the 1 digit?
lets work on problem 1 from lecture notes 4 as our example. A particle with a...
lets work on problem 1 from lecture notes 4 as our example. A particle with a mass of 5.00 grams and a charge of 4 microcoulombs has a speed of 0.75 m/s when it passes through a point at which the potential is -1200 volts electron and a proton are released form rest in a uniform electric field that has a magnitude of 500 N/C. The energy and speed of each particle is measured after it has moved through a...
1. Implement the recursive factorial function given below (from the lecture notes on recursion). Develop a...
1. Implement the recursive factorial function given below (from the lecture notes on recursion). Develop a main program that allows the user to enter any integer (positive) integer value, and displays the factorial of that value. The program should allow the user to either keep entering another value, or quit the program. public static int factorial(int n) { if (n == 0 || n == 1) return (1); else return (n * factorial(n-1)); } 2. Determine the largest value for...
Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers)...
Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers) i := 1 while (i ≤ n and x 6= ai) i := i + 1 if i ≤ n then location:= i else location:= 0 return location Apply the linear search algorithm to search for the number 3 in the list 1, 5, 3, 9. (a) In this application of the algorithm, what is the integer n? (b) What is the initial value...
4. Recall the cookie problem from lecture. We have two bowls, Bowl 1 and Bowl 2....
4. Recall the cookie problem from lecture. We have two bowls, Bowl 1 and Bowl 2. Bowl 1 contains 25% chocolate and 75% vanilla cookies; Bowl 2 has 50% of each. For this problem, assume each bowl is large enough that drawing a single cookie does not appreciably alter this ratio. Suppose we draw two cookies from the bowl and they are both chocolate. Calculate the posterior probabilities of the two bowls in two ways: (a) by treating the two...
Project #1.  Goldbach Conjecture. “Every even integer greater than 2 can be represented as the sum of...
Project #1.  Goldbach Conjecture. “Every even integer greater than 2 can be represented as the sum of two prime numbers.” For this project show the sums from 100,000 to 100,200. (1) Print it as follows: 100,000 ​prime no. 1​prime no. 2 100,002​​…​​… 100,004​​…​​… …​​​…​​… …​​​…​​… 100,200​​…​​… (Of course, if you find that there is no such pair of primes, indicate the first number that does have a Goldbach pair. I don’t think you’ll find such a number!) (2) Also, print out...
Suppose the demand for good X can be represented by the following equation: QX = 35...
Suppose the demand for good X can be represented by the following equation: QX = 35 - 2P. Furthermore, suppose that the demand for good Y can be represented by    QY = 25 - 0.1P. a. Find the elasticity of demand for both good X and good Y when the price of X is $15 and the price of Y is $10. b.If the community’s goal is to raise tax revenue as efficiently as possible, what should be the ratio...
Dr Pepper’s preferences can be represented by the utility function u(x,y) = x + y where...
Dr Pepper’s preferences can be represented by the utility function u(x,y) = x + y where x is his consumption of Coca Cola (hereafter, referred to as Coke) and y is his consumption of orange juice (hereafter, referred to as OJ).   Initially, both types of drinks are not taxed and with an income of $12 he faces prices ($1, $2). On the advice of nutritionists, the government decides to impose a specific tax of $2 on Coke which leads to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT