Skip to content

Instantly share code, notes, and snippets.

@blackraccoon000
Last active December 15, 2015 11:09
Show Gist options
  • Save blackraccoon000/5250746 to your computer and use it in GitHub Desktop.
Save blackraccoon000/5250746 to your computer and use it in GitHub Desktop.
2進数10進数変換(255 -> 11111111)想定
/**
* Created with IntelliJ IDEA.
* User: Yutaka Fujii
* Date: 13/03/27
* Time: 9:24
* To change this template use File | Settings | File Templates.
*/
//得た10進数を2進数に変換する。
public class Bindec {
double x=0;
Boolean binFlag[] = new Boolean[8];
StringBuffer binStr = new StringBuffer();
void changeBin(double x){
for(int i = 0;i<8;i++){
binFlag[i] = false;
if(x > Math.pow(2,i)){
x = x - Math.pow(2,i);
binFlag[i] = true;
}
// System.out.println(i+":"+binFlag[i]);
if(binFlag[i] == true){
binStr.append(1);
} else if(binFlag[i] == false){
binStr.append(0);
}
}
System.out.println(binStr);
}
// void changeDec(double x){
// for(int i =1;i<9;i++){
//// String.valueOf(x).codePointAt(i);
// System.out.println(Integer.);
// }
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment