13.binary number system C programming
-
Upload
aircraft-manufacture-engineering-essential-book -
Category
Engineering
-
view
150 -
download
2
Transcript of 13.binary number system C programming
- 1. - (decimal) (binary) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 , 0 1 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 19, 20, 21, , 98, 99, 100, 101 ... , ( ) , ( 1 0- 1 , 9 09 , 09- 10), 0 9 ( 1 0- 1 ) : 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011 ... , 0- 1 ( ) , , , , , , ? 10 , 100 , 1000 , (10), (100), (1000), (10000) (100 = 1) , (101 = 10) , (102 = 100) , (20 = 1) , (21 = 2) , (22 = 4) , , , , , , : 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 10 , 1 + 1 , (10) , , 101 + 101 = ? , 1 + 1 = 10 0 1 (carry) , 0 + 0 = 0, 0- 1 1 , 1 + 1 = 10 ( carry ) 0 1 : 1010 1011 + 1011 = ? 10110 , 0 0 = 0, 1 0 = 1, 1 1 = 0, 0 1 = 1 , 23 15 ? 3- 1 ( 1 ), 13 5 = 8 , 1- 0 1 0- , 10 (), ( ) 110 101 = ? 0 1 1, 1 101- 1 1 = 0, 1 1 = 0 : 001 1 - - x 10 - , x 2 - : 1903 = 1 x 103 + 9 x 102 + 0 x 101 + 3 x 100
2. : 10110 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 Exponential Expression , : 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 = 22 , , ? char binary[] = "10110"; int len = 5; // 5 int decimal = 0; // , 0 2 binary[0] '1' ,- ?- 4 , 0 int position = 4; int indx; for(indx = 0; indx < len; indx++) { decimal = decimal + pow(2, position); position--; } , pow math.h ab - pow(a, b) : #include #include #include int main() { char binary[65]; int len, decimal, power, i; printf("Enter the binary number: "); scanf("%s", binary); decimal = 0; len = strlen(binary); power = len - 1; for(i = 0; i < len; i++) { decimal += pow(2, power); power--; } printf("Decimal value is %dn", decimal); return 0; } : . 10110 , ? 31 22 3. pow(2, position) position- , : decimal += binary[i] * pow(2, power); ? 10110- , : ['1', '0', '1', '1', '0'] binary[0]- pow(2, 4), binary[1]- pow(2, 3), , binary[4]- pow(2, 0) 10110- ? Decimal value is 1510 ? ? , decimal += binary[i] * pow(2, power); , binary[i]- '0' '1' ( '0' '1') '0' 48 '1' 49 '0' 0 '1' 1 ? '0' '0' = 48 48 = 0 '1' '0' = 49 48 = 1 ? binary[i] (binary[i] '0') ( ) 95 n- , 2n