[CrackCode] 5.2 Print the binary representation
来源:互联网 发布:用ss网络会不会被监控 编辑:程序博客网 时间:2024/04/30 04:30
Given a (decimal - e g 3.72) number that is passed in as a string, print the binary rep-resentation If the number can not be represented accurately in binary, print “ERROR”
Analysis:
This is a typical example of how to transform a decimal number to binary number. Basic idea is to divide the decimal number into 2 parts, i.e., the int part (e.g. 3), and the decimal part (e.g. 0.72).
For int part, similar approach of extracting numbers from int:
1. use %2 to get each digit from lowest bit to highest bit.
2. int right shift 1 position (=>>1).
3. construct the binary number (always add to the higher position of the current binary number)
Please refer to the code below for the process above.
For decimal part, use *2 approach. For example:
int n = 0.75
n*2 = 1.5
Therefore, the first digit of binary number after '.' is 1 (i.e. 0.1). After constructed the first digit, n= n*2-1 (remaining number would actually be *(2^2) before calculating the next digit)
When the remaining number ==1, the decimal number has been successfully transformed to binary number. Otherwise, the decimal number could not be transform to binary number.
public class Answer {public static String printBinary(String str){int intPart = Integer.parseInt(str.substring(0,str.indexOf('.')));double decPart = Double.parseDouble(str.substring(str.indexOf('.')));// transform int part to binaryString int_string = "";while(intPart>0){int r = intPart%2;intPart>>=1;int_string = r+int_string;}// transform dec part to binaryStringBuffer dec_string=new StringBuffer();while(decPart>0){if(dec_string.length()>32) return "ERROR!";if(decPart == 1){// exitdec_string.append((int)decPart);break;}double r = decPart*2;if(r>=1){dec_string.append("1");decPart = r-1;}else{dec_string.append("0");decPart = r;}}return (int_string + "." + dec_string);}public static void main(String[] args) {String n = "19.25";String bs = printBinary(n);System.out.println(bs);}
- [CrackCode] 5.2 Print the binary representation
- Q5.2 Print the binary representation
- [CrackCode] 5.3 Print the next smallest and next largest number
- Code to print the byte representation of program objects
- lintcode: Binary Representation
- [LintCode]Binary Representation
- The most elegant way to print a Binary Tree
- A smart way to count the ONEs in an integer's binary form representation
- [lintcode]二进制表示 ,Binary Representation
- [CrackCode] 1.3 Remove the duplicate characters in a string
- Pretty print binary tree
- print binary tree boundary
- Print binary tree
- 655. Print Binary Tree
- 655. Print Binary Tree
- 655. Print Binary Tree
- Leetcode655. Print Binary Tree
- LeetCode Print Binary Tree
- Programming HIVE Chapter 7-14 读书笔记
- USACO 2010 Feb Chocolate Giving 领取巧克力
- hadoop namenode -format格式化名称节点时出错
- USACO 2009 Feb Revamping Trails 道路翻新
- 怒删了很多东西
- [CrackCode] 5.2 Print the binary representation
- USACO 2009 Feb Cruel Math Teacher 数的幂次
- [WebKit]WebCore之页面加载的设计与实现(二)
- POJ 3107 Godfather
- IDA + GDBServer实现iPhone程序远程调试
- C++ STL,ATL,WTL之间的联系和区别
- poll的设计
- hadoop:start-all.sh 启动时出错
- USACO 2012 Mar Haybale Restacking 重排干草