# Binary to decimal and decimal to binary Algorithms

Source: Internet
Author: User

Convert decimal to binary:
The result is 1 after 2 rounds.
Write the remainder and the last 1 in descending order as the result.
For example, 302
302/2 = 151 + 0
151/2 = 75 + 1
75/2 = 37 + 1
37/2 = 18 + 1
18/2 = 9 + 0
9/2 = 4 + 1
4/2 = 2 + 0
2/2 = 1 + 0
The binary value is 100101110.

Convert binary to decimal
Start from the last digit and column it as 0th, 1, 2...
The number (0 or 1) of the nth digit multiplied by the Npower of 2
Example: 01101011. Convert to decimal:
0th bits: 1 multiplied by 2 to the power of 0 = 1
1 to the power of 2 = 2
0 multiplied by the 2's power = 0
1 multiplied by the power of 2 = 8
0 multiplied by the power of 2 = 0
1 multiplied by the power of 2 = 32
1 multiplied by the power of 2 = 64
0 multiplied by the power of 2 = 0
Then: 1 + 2 + 0
+ 8 + 0 + 32 + 64 + 0 = 107.
Binary 01101011 = decimal 107.

1. Convert binary to decimal
The basic practice of converting a binary number into a decimal number is to first write the binary number as an expansion of the weighting coefficient, and then sum the values according to the decimal addition Rules. This method is called "adding weights.

Ii. Convert decimal number to binary number
When converting a decimal number to a binary number, because the integer and decimal number conversion methods are different, first convert the integer and decimal part of the decimal number, and then merge them.
1. convert a decimal integer to a binary integer
To convert a decimal integer to a binary integer, use the "Division 2 remainder, reverse order" method. The specific method is: remove the decimal integer with 2 to get a quotient and remainder; then remove the quotient with 2 to get a quotient and remainder, so that until the quotient is zero, then, the obtained remainder is used as the low-level valid bit of the binary number, and the obtained remainder is arranged in sequence as the high-level valid bit of the binary number.

2. Convert decimal to binary decimal
To convert decimal places into binary decimal places, use the "take 2 as an integer and arrange them in sequence" method. The specific method is: Use 2 to multiply decimal places to get the product, take out the integer part of the product, and then use 2 to multiply the remaining decimal part to get another product, then, the integer part of the product is taken out until the fractional part in the product is zero or reaches the required precision.
Then, sort the retrieved integers in order. The first integer is used as the high valid bits of the binary decimal places, and the second integer is used as the low valid bits.
Respondent: HackerKinsn-trial level 1 2-24

1. Binary and decimal conversion
(1) convert binary to decimal
Method: "sum by right"
Example:
(1011.01) 2 = (1x23 + 0x22 + 1x21 + 1x20 + 0x2-1 + 1X2-2) 10
= (8 + 0 + 2 + 1 + 0 + 0.25) 10
= (11.25) 10
(2) convert decimal to binary

· Convert a decimal integer to a binary number: "divide by 2 to return the remainder, and output in reverse order"
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 to binary: "multiply by 2 to get an integer and output it in sequence"
Example:
(0.625) 10 = (0.101) 2
0.625
X 2
1.25
X 2
0.5
X 2
1.0
2. Conversion between octal and binary
For example, convert 37.416 of octal to binary:
37. 4 1 6
011 111. 100 001 110
That is, (37.416) 8 = (11111.10000111) 2
For example, convert binary 10110.0011 to octal:
0 1 0 1 1 0. 0 0 1 0 0

That is, (10110.011) 2 = (26.14) 8
For example, convert hexadecimal number 5DF. 9 to binary:
5 d f. 9
0101 1101 1111.1001
That is: (5DF. 9) 16 = (10111011111.1001) 2

For example, convert binary 1100001.111 to hexadecimal:
0110 0001. 1110
6. E
That is, (1100001.111) 2 = (61.E) 16

Related Keywords:

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.