logo

Binární až desetinné číslo v C

Tato část se bude zabývat převodem binárních čísel na desítková čísla. Než přejdeme ke konceptu, musíme porozumět binárním číslům a desetinným číslům. Jak víme, počítač nerozumí slovům nebo číslům, které lidé píší nebo dělají. Místo toho rozumí pouze 0 a 1. Když například na počítači napíšeme slovo nebo číslo, různé programy nebo kompilátory pomohou tato čísla nebo slova převést do binárního tvaru (bit 0s a 1s). Aby tomu počítačový stroj snadno porozuměl.

np.sum
Binární až desetinné číslo v C

Binární číslo

Binární číslo je číslo, které představuje informace nebo data uložená v počítači s kombinací bitů 0s a 1s. Je také známá jako základní 2 číselná soustava, protože má dva bity, 0s a 1s. Jedná se o binární čísla (0 a 1) 1001, 1010, 1101, 1111, 1010101 atd.

Desetinné číslo

Desetinné číslo je číslo, které obsahuje 10 číslic od 0 do 9. Jeho základ je 10, protože shromažďuje 10 číslic (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) a představuje nebo tvoří celé číslo pomocí těchto deseti číslic.

Algoritmus pro převod binárního na desítkové

  1. Jako vstup vezměte binární číslo.
  2. Vydělte číslo 10 a zbytek uložte do proměnné rem.
  3. decimal_num = decimal_num + rem * základ;
    Zpočátku je decimal_num 0 a základ je 1, kde proměnná rem ukládá zbytek čísla.
  4. Vydělte podíl původního čísla 10.
  5. Vynásobte základ 2.
  6. Vytiskněte desetinné číslo binárního čísla.

Převeďte binární číslo na desítkové pomocí cyklu while

Uvažujme program C pro převod kombinace binárního čísla (0s a 1s) na desítkové číslo pomocí cyklu while.

program.c

 #include #include void main() { // declaration of variables int num, binary_num, decimal_num = 0, base = 1, rem; printf (' Enter a binary number with the combination of 0s and 1s 
'); scanf (' %d', &num); // accept the binary number (0s and 1s) binary_num = num; // assign the binary number to the binary_num variable while ( num > 0) { rem = num % 10; /* divide the binary number by 10 and store the remainder in rem variable. */ decimal_num = decimal_num + rem * base; num = num / 10; // divide the number with quotient base = base * 2; } printf ( ' The binary number is %d 	', binary_num); // print the binary number printf (' 
 The decimal number is %d 	', decimal_num); // print the decimal getch(); } 

Výstup

 Enter a binary number with the combination of 0s and 1s 1101 The binary number is 1101 The decimal number is 13 

Vysvětlení kódu: Jak můžeme vidět ve výše uvedeném programu, žádá od uživatelů binární číslo (0s a 1s), aby číslo uložil do proměnné num. Při každé iteraci cyklus while zkontroluje podmínku binárního čísla a ověří, že dané číslo by nemělo být menší než 0; jinak opustí smyčku.

Následuje iterace cyklu while takto:

1. iterace:

rem = 1101 % 10 => 1

decimal_num = 0 + 1 * 1 => 1 (desetinné_val = 0, rem = 1 a základ = 1)

číslo = 1101 / 10 => 110

základ = 1 * 2 => 2

2. iterace:

arraylist java

rem = 110 % 10 => 0

decimal_num = 1 + 0 * 2 => 1 (desetinné_val = 1, rem = 0 a základ = 2)

číslo = 110 / 10 => 11

java system.out.println

základ = 2 * 2 => 4

3. iterace:

rem = 11 % 10 => 1

decimal_num = 1 + 1 * 4 => 5 (desetinné_val = 1, rem = 1 a základ = 4)

číslo = 11/10 => 1

základ = 4 * 2 => 8

4. iterace:

zip v linuxu

rem = 1 % 10 => 1

decimal_num = 5 + 1 * 8 => 1 (desetinné_val = 5, rem = 1 a základ = 8)

číslo = 1/10 => 0

základ = 8 * 2 => 16

Převeďte binární číslo na desítkové pomocí cyklu for

Uvažujme program v jazyce C pro převod kombinace binárního čísla (0s a 1s) na desítkové číslo pomocí cyklu for.

desetinný.c

 #include #include #include // use math.h header file void main() { // declaration of local variables i, bin_num, decimal_num = 0, rem; int i, bin_num, decimal_num = 0, rem; printf (' Enter the binary number with the combination of 0s and 1s 
'); scanf ('%d', &bin_num); // takes the binary number as the input printf( ' 
 The binary number is %d', bin_num); // print the binary number for (i = 0; bin_num != 0; ++i) { rem = bin_num % 10; bin_num = bin_num / 10; decimal_num = decimal_num + (rem) * ( pow (2, i)); } // print the decimal number printf ('
 Conversion from binary to decimal number is %d', decimal_num); getch(); } 

Výstup

co je hibernace
 Enter the binary number with the combination of 0s and 1s 10010 The binary number is 10010 Conversion from binary to decimal number is 18 

Převeďte binární číslo na desítkové pomocí funkce

Uvažujme program v jazyce C, který pomocí uživatelem definované funkce převede kombinaci binárního čísla (0s a 1s) na desítkové číslo.

tak

 #include #include int binaryTodecimal(int bin_num); int main() { // declare the local variable int bin_num, dec_num; printf (' Enter the binary number (0s and 1s) 
'); scanf ('%d', &bin_num); dec_num = binaryTodecimal (bin_num); // call the binaryTodecimal() function printf (' Conversion of the binary number to decimal number is %d', dec_num); } // use user defined function --- binaryTo decimal function int binaryTodecimal( int bin_num) { // declaration of variables int decimal_num = 0, temp = 0, rem; while (bin_num != 0) { rem = bin_num % 10; bin_num = bin_num / 10; decimal_num = decimal_num + rem * pow( 2, temp); temp++; } return decimal_num; } 

Výstup

 Enter the binary number (0s and 1s) 11001 Conversion of the binary number to decimal number is 25 

Převeďte binární číslo na desítkové pomocí pole a funkce

Uvažujme program v jazyce C, který pomocí funkce a pole převede kombinaci binárního čísla (0s a 1s) na desítkové číslo.

Desetinné 2.c

 #include #include int binaryTodecimal (char num[]) { int i, deci_num, mul = 0; for ( deci_num = 0, i = str_length(num) - 1; i >= 0; --i, ++mul) { deci_num = deci_num + (num[i] - 48) * (1 << mul); } return deci_num; } int str_length( char str[]) { int i = 0; while (str[i] != '') i++; return i; } int main() { char num[] = '1101'; int deci_num; printf ('
 The binary number is %s', num); printf ('
 The decimal number of %s is %d', num, binaryTodecimal(num)); return 0; } 

Výstup

 The binary number is 1101 The decimal number of 1101 is 13