022 indicates the decimal point of a string type (string), and prints its binary representation (keep it up), 022 string

Source: Internet
Author: User

022 indicates the decimal point of a string type (string), and prints its binary representation (keep it up), 022 string
Returns the decimal point of a string type.
Note the validity of the string.
However, the following code does not process the number of decimals in an infinite loop,

When an infinite loop decimal number occurs, while (other> 0) may always be true

Code:

#include <iostream>#include <string>std::string to_binary_string(const std::string& vNumStr){std::string::size_type Pos = vNumStr.find('.');std::string IntPart = vNumStr.substr(0, Pos);std::string OtherPart = vNumStr.substr(Pos, vNumStr.length()-Pos);if (IntPart != ""){int Num = atoi(IntPart.c_str());IntPart = "";while (Num){if (Num&1) IntPart = "1" + IntPart;else IntPart = "0" + IntPart;Num >>= 1;}}    if (OtherPart.size() > 1){int Other = atof(OtherPart.c_str());OtherPart = "";while (Other>0){Other *= 2;if (Other>=1){OtherPart += "1";Other -= 1;}else{OtherPart += "0";}}}return OtherPart.size() > 1 ? IntPart + OtherPart : IntPart;}



How do decimal places be represented in binary format?

You can retrieve a decimal point such as 235.725, that is, 0.725. Multiply it by the decimal number and multiply it by 2 to get 1. 45. Take the integer part 1 as the first (very bit) of the binary decimal place, and place the decimal part 0. 45 multiplied by 2 to 0. 9. Take the integer part as the second (percentile) 0 of the binary decimal place, and set the decimal part to 0. 9 by 2, get 1. 8. Set the integer part to the third (decimal place) 1 of the binary decimal place and the decimal part to 0. 8 then multiply by 2 ...... And so on, until the value is 0 or a circular decimal number is formed.

How to use binary to represent a decimal number

For example, 5.6

Integer Part 5 is in the binary format of 101.

Decimal part 0.6
There is a formula like this: * 2. If <1 is 0, the base number = the base number. If it is greater than 1, the base number is 1. The base number = the base number-1.

0.6*2 = 1.2> 0, then the base number is 1 = 1.2-1 = 0.2.

0.2*2 = 0.4 <0 then 0, base = 0.4

0.4*2 = 0.8 <0, then 0, base = 0.8

0.8*2 = 1.6> 0 is 1, and the base is 1.6-1 = 0.6.
:
:
:
:
So 5.6 can be expressed as: 101.1001
It is more accurate to multiply below

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.