Python3 Tenth-How to convert into a binary

Source: Internet
Author: User
Tags decimal to binary time 0

In the world of Calculators 2 is the mainstream, and in the natural world of human 10 is the mainstream, then there will inevitably exist in the transformation of the problem. In this chapter we will talk about the transformation of the system, but also hope to deepen your understanding of the previous chapters of the knowledge in the chapter.

Principle: First of all, say about the concept of a bit of power, the number of symbols in a system of the numbers represented by the number of the numeric symbol value is multiplied by a number symbol for the constant, the constant is called "right". The size of the right is based on the base, where the number symbol is the integer power of the exponential number. The decimal number of the Hundred, 10, single, very bit right is 10 of 2, 10 1, 10 0, 10-1-square. The binary number is the n power of 2. The right expansion sum ( weighted addition) is the non-decimal decimal method.

1. Other binary decimal

The binary number is first written as the weighted coefficient expansion and then summed by the decimal addition rule:

Example: 1101 = 1*23 + 1*22 + 0*21 + 1*20 = 1*8 + 1*4 + 0*2 + 1*1 = 8 + 4 + 0 + 1 = 13

Similarly, octal decimal can also be added by using the weighted add method:

Example: 0o12 = 1 * 81 + 2 * 80 = 1 * 8 + 2 * 1 = 10

Look again, hexadecimal to decimal, how to use the weighted addition method:

Example: 0x12 = 1 * 161 + 2 * 160 = 1 * 16 + 2 * 1 = 18

2, decimal to other binary

As we all know, the cardinality of the binary is 2, and the 2 that we divide into decimal binary is its cardinality.

Let's start with an example of converting a decimal integer to a binary integer, assuming that the decimal integer A has a binary number of EDCBA in the form of a, then use the above method to expand the A=a (2^0) +b (2^1) +c (2^2) +d (2^3) +e (2^4) (and not just the process of decimal)


Assuming that the number is not converted to binary, divided by the cardinality 2
A/2=a (2^0)/2+b (2^1)/2+c (2^2)/2+d (2^3)/2+e (2^4)/2
Note: A except not open two, the rest! Other absolutely can be apart, because they all contain 2, and a by 1, he does not include factor 2, only the remaining.
Business:
B (2^0) +c (2^1) +d (2^2) +e (2^3), divided by the cardinality of more than 2 B, and so on.
When this number can no longer be removed by 2, the first remaining number of a is lower in the original number, and later the remainder is high, so all the remainder is written in turn. It's just EDCBA.

Let's take a look at how decimal goes to binary, taking 13 as an example:

Decimal integers are converted to binary integers using the " divide 2, reverse order " method. The specific method is: divisible by 2 decimal integers, you can get a quotient and the remainder, and then 2 to remove the quotient, and then get a quotient and the remainder, so that until the quotient is 0 o'clock, and then the first obtained remainder as a binary number of the low-effective bit, the remainder as a binary number of the high-level effective bit, and then arranged together

So, 0b1101 =

Let's take a look at how decimal goes to octal, taking 53 as an example:

Decimal integers are converted to octal integers using the " divide 8, reverse order" method. By dividing the decimal integer by 8, a quotient and a remainder can be obtained, and a quotient and a remainder will be obtained, and then it is done until the quotient is 0 o'clock, and then the first obtained remainder is used as the low effective bit of the octal number, and the remainder is the high effective bit of the eight binary number. Arrange them in turn.

So, 153 = 0o231

Through the above decimal to binary, and decimal to octal example, you should understand the law, you try to turn the decimal 286 into hexadecimal

3, decimal decimals converted to binary decimals

Decimal decimals are converted into binary decimals using the " multiply 2 rounding, Order " method. The specific method is: with 2 times decimal decimals, you can get the product, the integral part of the integer, and then 2 times the remainder of the fractional part, and then get a product, and then the integral part of the integer out, so, until the fractional part of the product is divided into 0, at this time 0 or 1 is the last of the binary. Or to achieve the required accuracy.
The integer part is then sorted in order, the first integer as the high-effective bit of the binary decimal, followed by the integer as the low-effective bit.
Decimal decimal to binary binary


such as: 0.625=0b0.101
0.625*2=1.25====== take out the integer part 1
0.25*2=0.5======== take out the integer part 0
0.5*2=1========== take out the integer part 1


Again such as: 0.7=0b0.1 0110 0110 ...
0.7*2=1.4======== take out the integer part 1
0.4*2=0.8======== take out the integer part 0
0.8*2=1.6======== take out the integer part 1
0.6*2=1.2======== take out the integer part 1
0.2*2=0.4======== take out the integer part 0
0.4*2=0.8======== take out the integer part 0
0.8*2=1.6======== take out the integer part 1
0.6*2=1.2======== take out the integer part 1
0.2*2=0.4======== take out the integer part 0


Principle:
About decimal decimals converting to binary decimals
Suppose that a decimal decimal B is in the form of a binary decimal 0.ab, the same as the right to expand, to be
B=a (2^-1) +b (2^-2)
Because the bit weights of fractional parts are negative powers, we can only multiply by 2 to
2b=a+b (2^-1)
Note that a becomes an integral part, and we take an integer exactly to get a, and the rest of the fractional part is the same.
It is worth mentioning that the fractional part of the right expansion of the number of digits is exactly the same as the integer portion, so there is no need to reverse the remainder.

What if the integer part of the decimal has an integer greater than 0? As the above integers are converted to binary, decimals are converted to binary, and then added together, for example: 6.125 turns into binary

6.125 = 0b110.001

Converting decimal decimals to octal decimals, decimal decimals to 16 decimal places is a similar operation, please convert 6.125 to 8 binary, 16 binary separately .

Think about how to convert 10 into any of the 2~36 of the system? Later in the tutorial, we teach you how to use the program to achieve.

Online Verification Tool: http://tool.oschina.net/hexconvert/

Python3 Tenth-How to convert into a binary

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.