Binary to decimal, decimal to binary algorithm __ algorithm

Source: Internet
Author: User
Tags binary to decimal decimal to binary integer


Binary turn decimal, decimal to binary algorithm

Decimal Turn binary:
Divide by 2 to the result of 1
To write the remainder and the last 1 backwards from the bottom up is the result.
For example 302
302/2 = 151 more than 0
151/2 = 75 more than 1
75/2 = 37 more than 1
37/2 = 18 more than 1
18/2 = 9 more than 0
9/2 = 4 more than 1
4/2 = 2 more than 0
2/2 = 1 more than 0
So the binary is 100101110

Binary goto Decimal
From the last one to count, ranked No. 0, 1, 2 ... bit
The number of nth digits (0 or 1) multiplied by the n-th square of 2
The results are summed up as answers.
For example: 01101011. Go to decimal:
No. 0 bit: 1 times 2 of 0 square =1
1 times 2 of 1 square =2
0 times 2 of 2 square =0
1 times 2 of 3 square =8
0 times 2 of 4 square =0
1 times 2 of 5 square =32
1 times 2 of 6 square =64
0 times 2 of 7 square =0
Then: 1+2+0
+8+0+32+64+0=107.
Binary 01101011 = Decimal 107.

One or two binary number converted to decimal number
The basic practice of converting a binary number to a decimal number is to first write the binary number as the weighted coefficient expansion, and then sum it by the decimal addition rule. This practice is known as the "weighted addition" method.

Second, decimal number converted to binary number
When a decimal number is converted to a binary number, the integer and fractional parts of the decimal number are converted before merging because of the different conversion methods of integers and decimals.
1. Decimal integers are converted to binary integers
Decimal integers are converted to binary integers using the "Divide 2, Reverse order" method. The practice is: with 2 to remove the decimal integer, 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, arranged in order

2. 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 out, and then 2 by the remainder of the fractional part, and get a product, and then the integral part of the product is taken out, so, until the fractional part of the product is divided into 0, or to achieve the required precision.
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.
Respondents: HACKERKINSN-trial level 2-24 13:31

1. Conversion of binary to decimal
(1) Binary Turn decimal
Method: "Expand sum by right"
Cases:
(1011.01) 2 = (1x23+0x22+1x21+1x20+0x2-1+1x2-2) 10
= (8+0+2+1+0+0.25) 10
= (11.25) 10
(2) Decimal turn binary

· Decimal integer to binary number: "Divided by 2, reverse output"
Example: (89) 10 = (1011001) 2
2 89
2 44 ... 1
2 22 ... 0
2 11 ... 0
2 5 ... 1
2 2 ... 1
2 1 ... 0
0 ... 1
· Decimal fractional to binary number: "Multiplied by 2 rounding, sequential output"
Cases:
(0. 625) 10 = (0. 101) 2
0. 625
X 2
1. 25
X 2
0. 5
X 2
1. 0
2. Octal vs. binary conversion
Example: Converting an octal 37.416 to a binary number:
37. 4 1 6
011 111. 100 001 110
i.e.: (37.416) 8 = (11111.10000111) 2
Example: Converting binary 10110.0011 to octal:
0 1 0 1 1 0. 0 0 1 1 0 0
2 6. 1 4
i.e.: (10110.011) 2 = (26.14) 8
3. Hexadecimal vs. binary conversion
Example: converting hexadecimal number 5df.9 to binary:
5 D F. 9
0101 1101 1111. 1001
i.e.: (5df.9) 16 = (10111011111.1001) 2

Example: Converting binary number 1100001.111 to 16 binary:
0110 0001. 1110
6 1. E
namely: (1100001.111) 2 = (61. E) 16

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.