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. lfs遇到的一些问题--后续阶段

    1.安装GPM-1.20.7,make install出错: prog/display-buttons.c:39:57: 致命错误:gpm.h:没有那个文件或目录 #include <gpm.h ...

  2. mao/reduce实现求平均值

    import java.io.*; import java.util.*; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io. ...

  3. HW4.28

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  4. 腾讯sdk配置

    android-mirror.bugly.qq.com

  5. linux内核数据结构--进程相关

    linux里面,有一个结构体task_struct,也叫“进程描述符”的数据结构,它包含了与进程相关的所有信息,它非常复杂,每一个字段都可能与一个功能相关,所以大部分细节不在我的研究范围之内,在这篇文 ...

  6. 模仿GsonConverter 写的StringConverter 解析String字符串

    使用自己写的StringConverter 来封装的 Converter 来解析String private static final RestAdapter CAMERA_CLIENT_NETWOR ...

  7. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  8. 【python自动化第一篇:python介绍与入门】

    一.python介绍以及发展史  1.1 python的介绍: 简单点来说吧,python这玩意儿是一个叫做Guido van Rossum的程序猿在1989年的圣诞打发时间而决心去开发的一个脚本编程 ...

  9. Quartz定时任务学习(七)Cron 触发器

    Cron表达式 Quartz使用类似于Linux下的Cron表达式定义时间规则,Cron表达式由6或7个由空格分隔的时间字段组成,如表1所示: 位置 时间域名 允许值 允许的特殊字符 1 秒 0-59 ...

  10. 用JAX-WS在Tomcat中公布WebService

    JDK中已经内置了Webservice公布,只是要用Tomcat等Webserver公布WebService,还须要用第三方Webservice框架. Axis2和CXF是眼下最流行的Webservi ...