[Introduction to programming in Java 笔记] 1.3.7 Converting to binary 十进制到二进制的转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Introduction to programming in Java 笔记] 1.3.7 Converting to binary 十进制到二进制的转换相关的知识,希望对你有一定的参考价值。
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 十进制到二进制的转换的主要内容,如果未能解决你的问题,请参考以下文章
[转帖]Programmer’s guide to the big tech companies 💻
Analysis of algorithms: introduction
To Be A Good Programmer? All it takes is these 10 habits !
To Be A Good Programmer? All it takes is these 10 habits !