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

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?
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
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...
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...
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)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT