public class Binary
{
public static void main(String[] args)
{ // Print binary representation of N.
int N = Integer.parseInt(args[0]);
int v = 1;
while(v <= N/2)
v = 2*v;
// Now v is the largest power of 2 <= N.
int n = N; // current excess
while (v > 0)
{ //Cast out the power of 2 in decreasing order.
if (n < v) { System.out.print(0); }
else { System.out.print(1); n-=v;}
v = v/2;
}
System.out.println();
}
}

打印10进制数字(decimal number)的二进制表示。将数字拆成2的幂次的和的形式。例如 19 = 16 + 2 + 1. 所以 19 的二进制表示为 10011. 

  

[Introduction to programming in Java 笔记] 1.3.7 Converting to binary 十进制到二进制的转换的更多相关文章

  1. [Introduction to programming in Java 笔记] 1.3.9 Factoring integers 素因子分解

    素数 A prime is an integer greater than one whose only positive divisors are one and itself.整数的素因子分解是乘 ...

  2. [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

    赌徒赢得机会有多大? public class Gambler { public static void main(String[] args) { // Run T experiments that ...

  3. Thinking in Java——笔记(1)

    Introduction To Obejct The progress of abstraction But their primary abstraction still requires you ...

  4. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  5. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  6. Java笔记(一)

    Java语言特征 Java之父:詹姆斯,格斯林 跨平台:一次编译,到处运行write once,run everywhere! Java是一种面向对象的编程语言(OOP)面向对象(OO -- Orie ...

  7. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  8. 每天一本电子书 - JavaScript for Kids: A Playful Introduction to Programming

    JavaScript for Kids: A Playful Introduction to Programming 作者: Nick Morgan  出版社: No Starch Press 副标题 ...

  9. 2018-12-09 疑似bug_中文代码示例之Programming in Scala笔记第九十章

    续前文: 中文代码示例之Programming in Scala笔记第七八章 源文档库: program-in-chinese/Programming_in_Scala_study_notes_zh ...

随机推荐

  1. C语言练习题_北理工的恶龙

    背景:最近,北理工出现了一只恶龙,它长着很多 头,而且还会吐火,它将会把北理工烧成废墟, 于是,校长下令召集全校所有勇士杀死这只恶龙.要杀死这只龙,必须把它所有的头都砍掉,每个勇士只能砍一个龙头,龙的 ...

  2. 运行.class文件提示找不到或者无法加载主类原因

    在Java初学之时,用文本文件写了一个“hello world”的简单程序.在dos环境下使用命令javac -test1.java 进行编译. 编译出名称为test1.class的Java运行文件. ...

  3. malloc的实现

    在做csapp的malloc实验,一开始是按照书上的隐式链表法,发现得分很低.这种方法确实很挫,需要遍历一遍以找到合适的空闲块.于是我想到<STL源码剖析>中stl的内存池,感觉应该可以用 ...

  4. mysql 备份与还原

    http://dev.yesky.com/281/35291281.shtml 每一种逻辑错误发生的概率都极低,但是当多种可能性叠加的时候,小概率事件就 放大成很大的安全隐患,这时候备份的必要性就凸显 ...

  5. hug and Compression Resistance

    Hugging => content does not want to grow Compression Resistance => content does not want to sh ...

  6. mongoDB文件太大查错纪录

    日志系统,突然从24号之后的都断层了,交易看不见.查了一下问题是MongoDB把硬盘撑爆了,看了下情况: -bash-3.2$ du -h 82M ./log .1G ./db/journal .0K ...

  7. 【转】【阮一峰的网络日志】Git 使用规范流程

    作者: 阮一峰 日期: 2015年8月 5日 团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 否则,每个人都提交一堆杂乱无章的commit,项目很快就会变得难以协调和维护. 下面是Th ...

  8. 关于升级linux下apache

    1:Check whether Apache is already installed. # rpm -qa | grep -i http httpd-tools-2.2.8-3.i386 httpd ...

  9. Jquery Mobile左右滑动效果

    首先在一个页面里面定义两个< div data-role="page">,这里为了突出重点,就没有写出footer和header.定义的页面如下: <body&g ...

  10. PAT 1069. The Black Hole of Numbers (20)

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...