maxiaophp
BAN USER/**
* Write a program to convert decimal to 32-bit unsigned binary.
*/
public static String convertDecimalToBinary(float x){
StringBuffer sb = new StringBuffer();
int intX = (int)x;
float decX = x-intX;
int neg = 1 << 31;
int posX = intX;
int count = 0;
while((posX&neg) == 0){
posX <<= 1;
count++;
}
char posArray[] = new char[32-count];
System.out.println("count:"+count);
int index = posArray.length-1;
while(intX > 0){
posArray[index--] = (intX&1) == 1 ? '1':'0';
intX >>= 1;
}
sb.append(posArray);
sb.append(".");
while(Math.abs(decX) > 0.00001){
if(decX * 2 >= 1){
sb.append('1');
}else{
sb.append('0');
}
decX = decX*2-(int)(decX*2);
}
return sb.toString();
}
- maxiaophp September 11, 2013