Declare a package name, such as \"my_package\". In the package body, perform the
ID: 3929514 • Letter: D
Question
Declare a package name, such as "my_package". In the package body, perform the following steps. Write a function to convert a "bit_vector" into an "integer". The function should receive an input of "b¡t_vector" type and return an output of "integer" type. It is also necessary to perform the reverse operation. Write a function that converts an input of "integer" type to an output of "bit_vector" type. Overload the "+" operation for the following types of additions: a) bit_vector + bit_vector = bit_vector b) bit_vector + integer = bit_vectorExplanation / Answer
package my_package;
import java.util.*;
public class Simple{
public static boolean[] bit_vector2integer(int integer) {
int n = 32;
boolean[] bits = new boolean[n];
for(int i = 0; i < n; i++) {
if ((1 << n-i-1 & integer) != 0)
bits[i] = true;
}
return bits;
}
public static int bit_vector2integer(boolean[] bits) {
int ans = 0;
for(boolean i : bits){
ans = ans << 1 | (i?1:0);
}
return ans;
}
public static void main(String args[]){
int n = 15;
boolean[] bits = bit_vector2integer(n);
for (int i=0;i< 32 ; i ++ ) {
if(bits[i])
System.out.print("1");
else
System.out.print("0");
}
System.out.println("");
int ans = bit_vector2integer(bits);
System.out.println(ans+ "");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.