The meaning and algorithm of the binary system

Source: Internet
Author: User
Tags ming

1. Questions

1.1 Apple Boxing

Xiao Ming's family picked 123 apples, 10 apples can fill 1 boxes, 10 boxes of apples can be filled with 1 cars to the wholesale market to sell, asked how many of these apples can fill a few cars, a few boxes, and a few apples left?

A smart friend might give the answer directly to 1 cars and 2 boxes, with 3 apples left.

The process of calculation:

123 x/10 = 12 cases, 3 apples

12 Case/10 = 1 car, 2 cases

Of course, some friends will immediately point out, why so SB's count, 123 of the Hundred 1 is not 1 cars, 10 2 = 2 boxes, digit 3 = 3

Yes, that's the meaning of the 123 number. The hundred indicates the number of cars, 10 is the number of boxes, the digit indicates the number of apples left.

Suppose our apples must be boxed every 10, and sold (or rotten) every 10 cartons, and this packing truck is what we call carry.

1.2 Banks exchanging money

Xiao Ming now has 123 1 cent coins, want to go to the bank for the whole, finally can exchange 1.2 yuan 3 points.

Here the same, 1 yuan is the number of hundred, 2 cents is 10 digits, 3 points is the number of bits, this is the meaning of the digital representation.

2. Significance of position and mathematical concept

21,000 Energy formula

All decimal numbers can be represented by a formula:

Exponent n = number of digits in the position, single n=0, 10-bit n=1, incremented

and an = number on the corresponding digit

10 = Decimal

Then there is a universal formula to describe all the binary counts:

2.2 Another consideration of question 1.1

123 Apples, if the computer to calculate can fill a few cars, a few boxes, and a few apples left?

At this time the computer can not use the eyes to see the 123 figure of the meaning of the expression, but found that 1.1 of the computational process of the computer could be described:

A =  ten;           // A in turn equals a, box, car Ten ; Loop ...

And maybe some friends have an idea:

The car index n=2, looks like num/100 = 1 is the number of cars

But there are 2 questions:

    • The computer does not know the length of the number 123, assuming that we are 23 apples, and then remove 100 is the quotient is 0,0 generally is automatically ignored, so the step is redundant calculation
    • If the number of boxes is required, NUM/10 = 12, this is not the case.

If there is a similar problem in finding the remainder, the remainder is always right when the last remaining number of apples is asked:

123% 10 = 3 x

So that every time in addition to 10 of the operation, in turn, let the box, the car bit into bits, that is, the right shift in the programming, and then the rest is no problem, this time we would like to, if 10 in the system also has a displacement operator how good AH

2.3 10-Binary shift operation

When we divide num by 10 o'clock (the remainder is discarded and no decimals are processed), there is a magical phenomenon:

123/10 = 12

12/10 = 1

This is actually a right-shift operation, so the 10-in-right shift is to divide it by 10 (the left shifts it by 10), and the remaining number is the lowest bit value removed.

2.4 Mathematical Proofs (note that no decimal calculation is involved, remainder is discarded)

At this time A1 into a bit, next time to continue in addition to 10, will be removed from the A1

Thus, we can use this method to move the number to the right, and then move out of the remainder is the corresponding bit of the value of the number.

3. Application

The above said a bunch of formulas and theories, did not see what practical use, and the theory is to practice service, then let's see how it works

3.1 Calculating the Apple boxing problem in question 1.1

voidctestmath::loadapple (unsigned uNum) {intNSize =0; unsigned uapplearray[3] = {0};  while(UNum) {uapplearray[nsize]= uNum%Ten; UNum/=Ten; NSize++; } printf ("The apples can load to%d car,%d box, left%d apple\n", uapplearray[2], uapplearray[1], uapplearray[0]);}

3.2 Output a num in 10 characters (similar to Apple boxing problem)

intCtestmath::outputdecimalnum (unsigned uNum,Char*psznum) {    intNSize =0;  while(UNum) {psznum[nsize]= (uNum%Ten) +'0'; UNum/=Ten; NSize++; }    //because it is from the low start processing, so put the number of psznum is inverted, need to swap     for(inti =0; I < nSize/2; i++)    {        CharCtemp =Psznum[i]; Psznum[i]= Psznum[nsize-1-i]; Psznum[nsize-1-I] =ctemp; } Psznum[nsize]=NULL; returnNSize-1;}

4 16 Binary

4.1 Back to question 1.1, with Xiao Ming selling apples money, a new car, can pack 16 boxes of apples, at the same time increase the box, each box can pack 16 apples, at this time 123 apples can fill a few cars, a few boxes, and a few apples left?

At this point, we find that 123 of the literal can not directly give an answer, how to do?

Using a similar calculation method in 1.1:

123 x/16 = 7 box, 11 apples, 16 binary to B

7 Cases/16 = 0 cases, 7 boxes

So the answer is to be able to fill 0 cars, 7 cases, left B (11) Apples, recorded as the number "7B"

At this point, we look at the "7B" this number, you can use the 1.1 method to see that 10 "7" is the box number, B is the remaining number of apples, can be expected, the hundred is the number of cars, this is the actual meaning of the number of different numbers.

In fact, in our real life, we also have other applications, such as clocks, seconds to minutes, divided into hours, that is 60 binary, if you want to ask 123,456 seconds equals how many hours, how many points, how many seconds? You can use a similar algorithm to solve.

4.2 Mathematical Proofs

Go back to our universal formula and convert it into 16-binary form:

This formula still holds, so we can solve the problem in 16 binary in the same way

4.2 Convert 10 binary numbers to 16 decimal characters

intCtestmath::D ecimaltohexadecimal (unsigned uNum,Char*pszhexnum) {    intNSize =0; CharCtemp =0;  while(uNum) {ctemp= (uNum% -); //if the value is greater than 9, use a\b\c\d\e\f to indicate        if(Ctemp >9) {ctemp+= ('A'-Ten); }        Else{ctemp+='0'; } Pszhexnum[nsize]=ctemp; UNum/= -; NSize++; }    //because it is from the low start processing, so put the number of psznum is inverted, need to swap     for(inti =0; I < nSize/2; i++) {ctemp=Pszhexnum[i]; Pszhexnum[i]= Pszhexnum[nsize-1-i]; Pszhexnum[nsize-1-I] =ctemp; } Pszhexnum[nsize]=NULL; returnNSize-1;}

The meaning and algorithm of the 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.