Decimal number to binary representation

Source: Internet
Author: User

This article uses algorithms to convert a 10-digit integer to a binary number. Note: Here is the general binary conversion rule, which is not necessarily represented by all systems. Logically, if the decimal number 5 is represented in one byte, it should be 101. You can add 1 continuously through 000 to get this number. However, division is generally used. 5/2 obtain the result that the quotient is 2 remainder and 1 consumer is 2/2. The result that the quotient is 1 remainder and 0 is 0 and the quotient is 0 remainder. The result is 1 and the remainder is in reverse order. (the final result is placed in a high position) the connection is 101. this algorithm can be implemented recursively: [cpp] // Convert decimal integer in one byte to binary format string ByteToBinaryString (char v) {if (v = 1) {return "1";} if (v % 2 = 0) {return ByteToBinaryString (v/2) + "0";} else {return ByteToBinaryString (v/2) + "1" ;}} ByteToBinalyString to convert a 10-digit integer stored in a byte to a string expressed in binary format. Every time ByteToBinalyString is called, the quotient and remainder divided by 2 are calculated first, and the consumer continues to call himself recursively. After the remainder is connected to the return value of the next recursive function, a reverse order is achieved. When the outlet provider is 1, "1" is returned directly when 1 is used to call itself ". After testing, there is basically no problem in positive numbers. If it is 0, an error occurs. Because recursion becomes infinite, there is no exit. So we need to add some code to ensure that the program can work normally when 0 is directly passed as the parameter. [Cpp] // Convert decimal integer in one byte to binary format string ByteToBinaryString (char v) {if (v = 1) {return "1 ";} if (v = 0) {return "0";} if (v % 2 = 0) {return ByteToBinaryString (v/2) + "0 ";} else {return ByteToBinaryString (v/2) + "1" ;}} now considers the case where the char value is smaller than 0. Obviously, this program still needs to be modified.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.