Saturday, June 20, 2015

[Programming book chapter twelve] binary number.

We are reckoning for the life of the dasabhittika (decimal) number system is used. Using the computer duibhittika or binary (binary) number system. Dasabhittika have the ten digit numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and two binary 0 and 1. The binary number system in this chapter we will look at some basic things and from binary to decimal and decimal to binary conversion of fact. Desimale we count like this: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ... 19, 20, 21, ..., 98, 99, 100, 101 .. .. Look, when we are on the right side of the room (single room) using ten digit commit, he left the house early the value of the sums one walk (if you do not have to add 1 to 1 to 0, and that tall or what, but the 9 and 09 the same thing, so the next number of 09 to 10), the home of the decade from 0 to 9, using the address of the amendment to the sums of the century house (and if you do not tall or 0 to 1, with 1 to add more) . We'll count the same binary: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011 .... Only two of the address, so the two on the left side of the drawing room, with one to replace or add 1 to 0 (on the left side, we can not arbitrarily impose zero). However, units of the binary system we have, of late, century houses, one would say that tens of thousands of houses, two rooms, four rooms, room eighties. Why so? Think a little. Desimale it takes 10 to enter the address, enter the 100 three, four in 1000, to write, so write in binary two-two (10) to enter the four-three (100), enter eight to four (1000), sixteen to enter the five (10000) address to use. Desimale right of the first address (100 = 1), the unit cells, the address (101 = 10) of the decade, a third address (102 = 100) of the century, but also the right of the first binary digit (20 = 1) by the house, The following address (21 = 2), the two houses, then (22 = 4) of the four houses, like this. Dasabhittika example of addition, subtraction, multiplication, and can be shared, can be done in binary. In fact, you can not add, but the rest does not matter. Again these are binary fractions, but I'll only talk about integers. Accounts of the original inclusions are: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 10. As Decimal calculation, 1 + 1 on the case, either (10) first came to zero, is the one, after I wrote it. 101 + 101 = How many? First, add another room, 1 + 1 = 10. So, sit in the house by the sum of 0 and remained in the hands of 1 (carry). Now the two house, 0 + 0 = 0, so this will add 1 to 0 with the hand. 1. If the sum of the two to sit in the house. Now, if we get four houses, 1 + 1 = 10. There is nothing at hand (there is no carry). So it will be a 0 and 1 will be a four room house eighties. Sum: 1010. 1011 + 1011 = Tell me now, how much? Continue by adding the sum was 10110, then you made a mistake somewhere in the addition. Decimal would be calculated as in the case of substitution. 0 - 0 = 0, 1 - 0 = 1, 1 - 1 = 0, 0 - 1 = 1. The last note, and 23 - 15 at the time, what do we do? The 3-in-1 left a legendary assume (or borrowed 1), and 13 - 5 = 8 impressed. And instead of a loan made, it adds the value of the home 1. So binary 0 - 1 to 0 in the left one time, the number of 10 (two), these two will have one-to-one elimination. The next one will have to add a room (with the number being subtracted). 110 - 101 = How many? Excluding one-room from 0 to 1 is 1, and 1 has to borrow. So the two houses of 101 will add it. If the house between 1 - 1 = 0, four home 1 - 1 = 0. So the difference will be: 001 or 1. If you can add and subtract times-there is no reason why we can not share. Decimal as a rule. However, the number of decimal digits, we place the sum of the fixed address, such as x 10 can enter, the binary digits x 2 specific address can be written as the sum of the sums of the US. For example: 1903 = 1 x 103 + 9 x 102 + 0 x 101 + 3 x 100. Binary: 10110 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20. In English, it's called Exponential Expression. If you want to find out the value of any binary now desimale, the first to enter the binary number in the form of exponential expressions. Gunaphalagulo desimale then be calculated. See the example below: 10110 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 1 x 16 + 0 x 8 + 1 x 4 + 1 x 2 + 0 x 1 = 16 + 0 + 4 + 2 + 0 = 22 10110 = decimal 22 is binary. We do not have any programming for a long time, let the value of the binary number to decimal wrote a program. It has become known method. Now the important question is: What would I read binary numbers? We can use string. char binary [] = "10110"; int len ​​= 5; 5 // string length. int decimal = 0; // We have not yet calculated, so grabbed the decimal value is 0. We have the binary numbers in a loop with the amount of 2 times the amount that the US will join with decimals. The first character of the binary [0] on the '1' in the position of how are you? 'S position 4. The ensuing substantial decrease in the position of one of the standard, the position at the end of the digits 0. We are out of the loop when the decimal value of the binary number, we will get. Here, I've used the pow function. It is stated in the header file math.h. ab- out the value of the pow (a, b) be required to compile and execute hayaprograma. If you give input to 10110, then how come output? He should come to the output of the output of 31 to 22. If we made a mistake somewhere. You try to find the error manually. We are in the pow (2, position) multiplied by a binary number to the amount of the position-, we forgot it. In other words, we would write: decimal + = binary [i] * pow (2, power); I do notice a thing? 10110 Home of the amount by the end of our array of characters, and the first character of the array is the amount of sixteen houses. Thus the number of array ['1', '0', '1', '1', '0']. So binary [0] - triple with pow (2, 4), binary [1] - triple with pow (2, 3), ..., the binary [4] - with the triple pow (2, 0 ). Now, with the right program, then run. What is the output for the input of 10110? I can see the output Decimal value is 1510. Where was the mistake? I did all right. Again, you're not going to bother you? Test cricket needs such patience, game programming, so patience. The error that the decimal + = binary [i] * pow (2, power); There is no doubt that this statement. This is because we have changed a bit. Notice, binary [i] - the value of '0' or '1' (ie character '0' or 'Character' 1 '). Now the computer '0' understands that 48 of the '1' to understand 49. Jhamelata been here. Now this '0ke 0 and' How does she usually react 1ke 1? '0' - '0' = 48 - 48 = 0. '1' - '0' = 49 - 48 = 1. Is not that great idea? We are binary [i] writing (binary [i] - '0') to write the trouble. Now the program will work properly (unless you made a mistake if new). Now we look at the binary to decimal conversion. Show an example of the process. Who will have to convert binary is just 95. Now we have to find the maximum value of n-, where 2n <= 95. Paoyaragulo either 1, 2, 4, 8, 16, 32, 64, 128, .... Here we see the 64 <95 or 26 <95. 6. If the value of n-. And our seven-digit binary number (0 to 6, a total of seven digit). Since 64 <95, so this number can be taken. If sixty-four cells (first from left to right or from right to left in the seventh) to 1 (1xxxxxx). Now the value of n- 1 kamai. 64 + 25 = 64 + 32 = 96, which is greater than 95. Therefore, it can not be taken. So thirty-house 0 tall (10xxxxx). Now once again the value of the kamai n-, n-'s value is now 4. 64 + 24 = 64 + 16 = 80 <95. So will house sixteen 1 (101xxxx). Now the value of the kamai n-, n = 3. 80 + 23 = 80 + 8 = 88 <95. So, sit in the eighties cell 1 (1011xxx). And similarly, n = 2-for 88 + 22 = 88 + 4 = 92 <95. 1 four rooms sit (10111xx). Then n = 1, 92 + 21 = 92 + 2 = 94 <95. Two rooms 1 (101111x). Now, n = 0, 94 + 20 = 94 + 1 = 95. So one house 1. So the binary number 1011111. You are now entering the program to convert the binary number to any dasabhittika Fellow and examine the different standards. We'll do the same in a different way now. See the table below: Quotient Remainder 95/2 47 1 47/2 23 1 23/2 11 1 11/2 5 1 5/2 2 1 2/2 1 0 1/2 0 1 Then the remainder of the column to its own digits in order from the first binary number, we'll be getting lekhalei: 1011111. We'll do it as long as they are not part of the quotient 0 am. Binary to decimal conversion code in this way, you break down. If the code is not the transformation of the main functions that a separate function.

No comments:

Post a Comment