public class Bin {
/*
* 有两个32位整数n和m,请编写算法将m的二进制数位插入到n的二进制的第j到第i位,其中二进制的位数从低位数到高位且以0开始。
* 给定两个数int n和int m,同时给定int j和int i,意义如题所述,请返回操作后的数,保证n的第j到第i位均为零,且m的二进制位数小于等于i-j+1。
* 测试样例:
* 1024,19,2,6
* 返回:1100
*/
public int binInsert(int n, int m, int j, int i) {
// write code here
int x;
x = m<<j;
int y;
y = n|x;
return y;
}
/*
* 有一个介于0和1之间的实数,类型为double,返回它的二进制表示。如果该数字无法精确地用32位以内的二进制表示,返回“Error”。
* 给定一个double num,表示0到1的实数,请返回一个string,代表该数的二进制表示或者“Error”。
* 测试样例:
* 0.625
* 返回:0.101
* */
public String printBin(double num) {
// write code here
StringBuilder sb = new StringBuilder("0.");
double x = num;
while(x!=0 && sb.length()<32){
x=x*2;
if(x>=1){
sb.append("1");
x-=1;
}else{
sb.append("0");
}
}
if(sb.length()>=32){
return "Error";
}else{
return sb.toString();
}
}
public int[] getCloseNumber(int x) {
// write code here
int y = x;int x1=0,x2=0;
int k;
int count = 0;
int count1 = 0;
int[] a1 = new int[32];
int[] a2 = new int[32];
int[] a3 = new int[32];
while(y!=0){
k = y%2;
a1[count1++]=k;
if(k==1)
count++;
y = y/2;
}
for(int i = 0;i<count;i++){
a2[i] = 1;
}
for(int i = count;i<count1;i++){
a2[i] = 1;
}
for(int j = 0;j<count1-count;j++){
a3[j] = 0;
}
for(int j = count1-count;j<count1;j++){
a3[j] = 1;
}
for(int i = 0;i<count1;i++){
x1+=a2[i]*(Math.pow(2,i));
}
for(int i = 0;i<count1;i++){
x2+=a3[i]*(Math.pow(2,i));
}
int[] q = new int[2];
q[0] = x2;
q[1] = x1;
return q;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Bin().binInsert(1024, 19, 2, 6));
System.out.println(new Bin().printBin(0.625));
int [] q = new int[2];
q = new Bin().getCloseNumber(2);
System.out.println(q[0]+" "+q[1]);
}
}
/*
* 有两个32位整数n和m,请编写算法将m的二进制数位插入到n的二进制的第j到第i位,其中二进制的位数从低位数到高位且以0开始。
* 给定两个数int n和int m,同时给定int j和int i,意义如题所述,请返回操作后的数,保证n的第j到第i位均为零,且m的二进制位数小于等于i-j+1。
* 测试样例:
* 1024,19,2,6
* 返回:1100
*/
public int binInsert(int n, int m, int j, int i) {
// write code here
int x;
x = m<<j;
int y;
y = n|x;
return y;
}
/*
* 有一个介于0和1之间的实数,类型为double,返回它的二进制表示。如果该数字无法精确地用32位以内的二进制表示,返回“Error”。
* 给定一个double num,表示0到1的实数,请返回一个string,代表该数的二进制表示或者“Error”。
* 测试样例:
* 0.625
* 返回:0.101
* */
public String printBin(double num) {
// write code here
StringBuilder sb = new StringBuilder("0.");
double x = num;
while(x!=0 && sb.length()<32){
x=x*2;
if(x>=1){
sb.append("1");
x-=1;
}else{
sb.append("0");
}
}
if(sb.length()>=32){
return "Error";
}else{
return sb.toString();
}
}
public int[] getCloseNumber(int x) {
// write code here
int y = x;int x1=0,x2=0;
int k;
int count = 0;
int count1 = 0;
int[] a1 = new int[32];
int[] a2 = new int[32];
int[] a3 = new int[32];
while(y!=0){
k = y%2;
a1[count1++]=k;
if(k==1)
count++;
y = y/2;
}
for(int i = 0;i<count;i++){
a2[i] = 1;
}
for(int i = count;i<count1;i++){
a2[i] = 1;
}
for(int j = 0;j<count1-count;j++){
a3[j] = 0;
}
for(int j = count1-count;j<count1;j++){
a3[j] = 1;
}
for(int i = 0;i<count1;i++){
x1+=a2[i]*(Math.pow(2,i));
}
for(int i = 0;i<count1;i++){
x2+=a3[i]*(Math.pow(2,i));
}
int[] q = new int[2];
q[0] = x2;
q[1] = x1;
return q;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Bin().binInsert(1024, 19, 2, 6));
System.out.println(new Bin().printBin(0.625));
int [] q = new int[2];
q = new Bin().getCloseNumber(2);
System.out.println(q[0]+" "+q[1]);
}
}