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. HDOJ/HDU 1161 Eddy's mistakes(大写字母转换成小写字母)

    Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for e ...

  2. malloc的实现

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

  3. pac 文件使用到的javascript函数

    下面是可用于FindProxyForURL()函数体中的条件函数: 基于主机名的函数: isPlainHostName() dnsDomainIs() localHostOrDomainIs() is ...

  4. 批量Linux 网络安装环境建立工具cobbler/kickstart

    批量Linux 网络安装环境建立工具网络安装服务器套件:     Cobbler(Red Hat 2008年发布的项目)    Kickstart(Red Hat08年前项目,相关脚本令人望而却步,现 ...

  5. asp.net将本地Excel上传到服务器并把数据导入到数据库

    前台代码: <td class="formLabel"> 批量修改: </td> <td class="formInput"> ...

  6. Android Studio 环境配置优化

    一.插件 .ignore: 版本控制忽略文件高亮和补齐ADB Idea: ctrl + Shift + A 查找中添加常用卸载安装app的一些操作,无需命令行Android ButterKnife Z ...

  7. Sublime ctags 函数跳转插件安装

    Sublime Text安装插件的方法,主要有以下两种: 1. 直接通过下载安装包安装 在编辑器菜单中点击“Preferences”–“Browse Packages…”打开插件安装目录,然后把下载的 ...

  8. ABAP ALV表头的实现

    ABAP实现ALV表头的DEMO: 效果: 源代码: *&------------------------------------------------------------------- ...

  9. SQL SERVER 中如何将NULL转换为0

    select isnull(fieldname,0) from tablename 如果字段fieldname的值是null,则结果是0

  10. RichtextBox 行和列

    获得光标所在的行号 获得光标所在的列号 设置光标到指定行号 设置光标到指定列号 http://www.huifangseo.com/blog/6/7.html 获得光标所在的行号和列号 方法1 int ...