java实现十进制转为二进制
时间:2010-10-17 来源:sulog
public class TenToTwo {
public static String change(long DecNumber) {
String BinNumber = "";
for (int i = 64; i >= 0; i--) {
if (DecNumber / (long) (Math.pow(2, i)) == 1) {
BinNumber = BinNumber + "1";
DecNumber = (long) (DecNumber - Math.pow(2, i));
} else {
if (!BinNumber.equals("")) {
BinNumber = BinNumber + "0";
}
}
}
return BinNumber;
}
/**
* @param args
*/
public static void main(String[] args) {
long DecNumber = 456;
System.out.println(TenToTwo.change(DecNumber));
}
}
public static String change(long DecNumber) {
String BinNumber = "";
for (int i = 64; i >= 0; i--) {
if (DecNumber / (long) (Math.pow(2, i)) == 1) {
BinNumber = BinNumber + "1";
DecNumber = (long) (DecNumber - Math.pow(2, i));
} else {
if (!BinNumber.equals("")) {
BinNumber = BinNumber + "0";
}
}
}
return BinNumber;
}
/**
* @param args
*/
public static void main(String[] args) {
long DecNumber = 456;
System.out.println(TenToTwo.change(DecNumber));
}
}
相关阅读 更多 +