{"id":26468,"date":"2017-12-20T21:13:13","date_gmt":"2017-12-20T15:43:13","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26468"},"modified":"2017-12-20T21:13:13","modified_gmt":"2017-12-20T15:43:13","slug":"binary-representation-given-number","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/binary-representation-given-number\/","title":{"rendered":"C Programming-Binary representation of a given number"},"content":{"rendered":"<p>Write a program to print Binary representation of a given number.<\/p>\n<p><strong>Source:<\/strong> Microsoft Interview Set-3<\/p>\n<p><strong>Method 1: Iterative<\/strong><br \/>\nFor any number, we can check whether its \u2018i\u2019th bit is 0(OFF) or 1(ON) by bitwise \u00a0ANDing it with \u201c2^i\u201d (2 raise to i).<\/p>\n<pre>1) Let us take number 'NUM' and we want to check whether it's 0th bit is ON or OFF\t\r\n\tbit = 2 ^ 0 (0th bit)\r\n\tif  NUM &amp; bit == 1 means 0th bit is ON else 0th bit is OFF\r\n\r\n2) Similarly if we want to check whether 5th bit is ON or OFF\t\r\n\tbit = 2 ^ 5 (5th bit)\r\n\tif NUM &amp; bit == 1 means its 5th bit is ON else 5th bit is OFF.<\/pre>\n<p>Let us take unsigned integer (32 bit), which consist of 0-31 bits. To print binary representation of unsigned integer, start from 31th bit, check whether 31th bit is ON or OFF, if it is ON print \u201c1\u201d else print \u201c0\u201d. Now check whether 30th bit is ON or OFF, if it is ON print \u201c1\u201d else print \u201c0\u201d, do this for all bits from 31 to 0, finally we will get binary representation of number.<\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void bin(unsigned n)<br\/>{<br\/>    unsigned i;<br\/>    for (i = 1 &lt;&lt; 31; i &gt; 0; i = i \/ 2)<br\/>        (n &amp; i)? printf(&quot;1&quot;): printf(&quot;0&quot;);<br\/>}<br\/> <br\/>int main(void)<br\/>{<br\/>    bin(7);<br\/>    printf(&quot;\\n&quot;);<br\/>    bin(4);<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Method 2: Recursive<\/strong><br \/>\nFollowing is recursive method to print binary representation of \u2018NUM\u2019.<\/p>\n<pre>step 1) if NUM &gt; 1\r\n\ta) push NUM on stack\r\n\tb) recursively call function with 'NUM \/ 2'\r\nstep 2)\r\n\ta) pop NUM from stack, divide it by 2 and print it's remainder.<\/pre>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void bin(unsigned n)<br\/>{<br\/>    \/* step 1 *\/<br\/>    if (n &gt; 1)<br\/>        bin(n\/2);<br\/> <br\/>    \/* step 2 *\/<br\/>    printf(&quot;%d&quot;, n % 2);<br\/>}<br\/> <br\/>int main(void)<br\/>{<br\/>    bin(7);<br\/>    printf(&quot;\\n&quot;);<br\/>    bin(4);<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Binary representation of a given number &#8211; Bit Algorithm &#8211; Write a program to print Binary representation of a given number.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74852,79474,82927,71771],"tags":[78853,78868,78877,74354,74367,78864,70203,74347,70195,74343,74342,74371,78857,70194,78866,70186,70205,78860,78855,78880,76992,78862,78878,78879,78876,70196,70197,78858,70223,78871,70187,70189,78875,70198,78869,78872,78870,78861,78863,70214,78854,78865,78856,78874,78867,74377,74364,78873,78859,74348],"class_list":["post-26468","post","type-post","status-publish","format-standard","hentry","category-bit-algorithms","category-branch-and-bound","category-c-programming-2","category-cs-subjects","tag-algorithm-to-convert-decimal-to-binary-in-c","tag-ascii-table-binary","tag-binary-and-decimal-numbers","tag-binary-digits","tag-binary-math","tag-binary-no","tag-binary-number-system","tag-binary-number-system-in-computer","tag-binary-numbers","tag-binary-numbers-definition","tag-binary-numbers-in-computer","tag-binary-numbers-table","tag-binary-programming","tag-binary-system","tag-binary-system-example","tag-binary-to-decimal","tag-binary-to-decimal-conversion","tag-binary-to-decimal-number-system","tag-binary-to-decimal-program-in-c","tag-bit-to-hex","tag-c-program-to-convert-decimal-to-binary","tag-c-programming-binary","tag-conversion-of-binary-numbers","tag-conversion-of-decimal-to-binary","tag-conversion-of-hexadecimal-to-binary","tag-convert-binary-to-decimal","tag-convert-decimal-to-binary","tag-convert-hex-to-binary","tag-dec-to-binary","tag-decimal-number-system-in-computer","tag-decimal-to-binary","tag-decimal-to-binary-conversion","tag-decimal-to-binary-conversion-examples","tag-decimal-to-binary-converter","tag-decimal-to-hexadecimal-in-c","tag-define-binary-number-system","tag-example-of-binary-number","tag-hex-bit","tag-how-to-calculate-binary-numbers","tag-how-to-convert-decimal-to-binary","tag-how-to-convert-decimal-to-binary-in-c","tag-how-to-find-binary-number","tag-how-to-solve-binary-numbers","tag-how-to-use-binary-code","tag-what-are-binary-numbers","tag-what-is-a-binary-number","tag-what-is-binary","tag-what-is-binary-code","tag-what-is-binary-language","tag-what-is-binary-system"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26468"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26468\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}