论坛首页 入门技术论坛

java代码不用java API实现十进制转换成二进制

浏览 2467 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-10  
OO
package scpj2.three;

public class Binary {

	public static void main(String[] args){
		System.out.println(makeBinaryString(22));
		int aa= 1;
		int bb = 22; 
		bb >>>=aa;
		System.out.println("22的2进制: "+Integer.toBinaryString(22));
		System.out.println("24的2进制: "+Integer.toBinaryString(24));
		System.out.println("11的2进制: "+Integer.toBinaryString(11));
		System.out.println("bb >>>1= "+bb);
		System.out.println("22 & 1= "+(22 & 1));
		System.out.println("11 & 1= "+(11 & 1));
	}
	public static String makeBinaryString(int i){
		/*This section could have been optimized using
		 * StringBufer,but is pressented in this way for the
		 * sake of simplicity.
		 */
		String binary = "";
		do{
			System.out.println("i>>>前:"+i);
			int lowBit = (i & 1);
			System.out.println("低位lowBit=(i & 1):"+(i & 1));
			String newDigit = ((lowBit == 0) ? "0" : "1");
			binary = newDigit + binary;
			i >>>= 1;
			System.out.println("i>>>后:"+i);
		}while (i != 0);
		return binary;
	}
}

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics