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

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
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...
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...
Which expression can be used to check if an integer x is a multiple of 5...
Which expression can be used to check if an integer x is a multiple of 5 within the range 20 to 50 inclusively? Select one: a. (x >= 20 && x % 5 == 0) || (x <= 50 && x % 5 == 0) b. x <= 50 && x >= 20 && x % 5 == 0 c. 20 <= (x % 5) <= 50 d. (x % 5 == 0) && (20 <= x <= 50)
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...
Consumer Theory. A consumer has preferences over goods x and y that can be represented by...
Consumer Theory. A consumer has preferences over goods x and y that can be represented by the utility function ?(?,?) = ?+??(?) where ln is the (natural) log function. The consumer has income I (all to be spent on x and y) and the price of x and y are px and py respectively. (You may assume the “at least as good as x” set B(x) is a convex set, so the solution to the consumer’s problem will be a...
A Cartesian vector can be thought of as representing magnitudes along the x-, y-, and z-axes...
A Cartesian vector can be thought of as representing magnitudes along the x-, y-, and z-axes multiplied by a unit vector (i, j, k). For such cases, the dot product of two of these fectors {a} and {b} corresponds to the product of their magnitudes and the cosine of the angle between their tails as in {a}⋅ {b} = abcos(theta) The cross product yields another vector, {c} = {a} × {b} , which is perpendicular to the plane defined by...
Judy's preferences over x and y can be represented by the utility function: U=xy3 The price...
Judy's preferences over x and y can be represented by the utility function: U=xy3 The price of x is Px = 8, the price of y is Py = 4, and her income is I = 480. At Judy's optimal consumption bundle, What is her demand for x? What is her demand for y?
Consider the problem from Lecture 5 “Fracking in the Permian Basin.” Denote by X oil production...
Consider the problem from Lecture 5 “Fracking in the Permian Basin.” Denote by X oil production in New Mexico and by Y oil production in Texas. Let the joint distribution of X and Y be given by f(x,y) = 4xy for x,y ∈ [0,1] (a) Show that f(x,y) is a valid joint density function. (b) Find the marginal density function for X. (c) Find the conditional density function for X. (d) Find P(X ≤ 1 2|Y ≥ 3 4) (e)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT