<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":513,"date":"2021-07-13T17:48:41","date_gmt":"2021-07-13T17:48:41","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=513"},"modified":"2021-09-13T06:17:20","modified_gmt":"2021-09-13T06:17:20","slug":"how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/","title":{"rendered":"How to Count number of bits to be flipped to convert A to B ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\" class=\"color-pink\" style=\"text-align: justify;\">How to Count number of bits to be flipped to convert A to B ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Write the program to count number of bits needed to be flipped to convert \u2018a\u2019 to \u2018b\u2019. For Example given two numbers a = 6 and b = 12; then the output is 2\n<ul>\n<li><a href=\"https:\/\/www.wikitechy.com\/technology\/check-binary-representation-number-palindrome\/\" target=\"_blank\" rel=\"noopener\">Binary representation<\/a>\u00a0of a is 00000<b>11<\/b>0<\/li>\n<li>Binary representation of b is 00001100<\/li>\n<li>We need to flip highlighted two bits in a<\/li>\n<li>To make it b.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"explanation\" class=\"color-purple\">Explanation<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Calculate\u00a0XOR\u00a0of A and B.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Output\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code class=\"hljs\" data-lang=\"\"><span class=\"nt\">a_xor_b = A ^ B<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Count the set bits in the above Calculated XOR result.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Output\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code class=\"hljs scss\" data-lang=\"\"><span class=\"nt\"><span class=\"hljs-function\">CountSetBits<\/span>(a_xor_b)<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"sample-code-in-c\" class=\"color-purple\" style=\"text-align: justify;\">Sample Code in C#<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/ Count number of bits to be <br\/>\/\/ flipped to convert A into B <br\/>using System; <br\/><br\/>public class Count { <br\/>\t<br\/>\t\/\/ Function that count set bits <br\/>\tpublic static int countSetBits(int n) <br\/>\t{ <br\/>\t\tint count = 0; <br\/>\t\twhile (n != 0) { <br\/>\t\t\tcount += n &amp; 1; <br\/>\t\t\tn &gt;&gt;= 1; <br\/>\t\t} <br\/>\t\treturn count; <br\/>\t} <br\/><br\/>\t\/\/ Function that return <br\/>\t\/\/ count of flipped number <br\/>\tpublic static int FlippedCount(int a, int b) <br\/>\t{ <br\/>\t\/\/ Return count of set <br\/>\t\/\/ bits in a XOR b <br\/>\t\treturn countSetBits(a ^ b); <br\/>\t} <br\/>\t<br\/>\t\/\/ Driver code <br\/>\tpublic static void Main() <br\/>\t{ <br\/>\t\tint a = 17; <br\/>\t\tint b = 55; <br\/>\t\tConsole.WriteLine(FlippedCount(a, b)); <br\/>\t} <br\/>} <br\/><br\/>\/\/ This code is contributed by vt_m. <\/code><\/pre> <\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-purple\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"Output\">\n<div class=\"hddn\">\n<figure class=\"highlight\" style=\"text-align: justify;\">\n<pre><code class=\"hljs\" data-lang=\"\"><span class=\"nt\">3<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Write the program to count number of bits needed to be flipped&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[3028],"tags":[195,971,491,221,3663,3648,368,3642,3645,3649,203,199,214,198,363,3057,3643,3652,3644,3655,3639,3641,205,3658,3653,222,3657,3638,3660,3661,3654,3651,3656,3637,3662,3650,3636,3659,484,3054,3640,196,212,207,366,204,3647,15925,206,972,200,3646,3055,197,968,3056,285,969],"class_list":["post-513","post","type-post","status-publish","format-standard","hentry","category-data-structure","tag-accenture-interview-questions-and-answers","tag-altimetrik-india-pvt-ltd-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-bharti-airtel-interview-questions-and-answers","tag-bit-flipping-game-code","tag-bit-manipulation-in-c","tag-bmc-software-interview-questions-and-answers","tag-brian-kernighans-algorithm","tag-c-program-to-count-number-of-ones-and-zeros","tag-c-program-to-toggle-a-bit","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-te-interview-questions-and-answers","tag-count-number-of-1s-in-a-binary-number-java","tag-count-set-bits","tag-count-set-bits-in-a-number-c","tag-count-total-set-bits-in-all-numbers-from-1-to-n","tag-counting-bits","tag-counting-bits-set-by-lookup-table","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-flip-all-bits-in-c","tag-flip-bits","tag-flipkart-interview-questions-and-answers","tag-flipped-mountain-code-in-c","tag-flipped-mountain-code-in-java","tag-flipped-mountain-in-c","tag-flipped-mountain-pattern-program-in-java","tag-flipped-mountain-program-in-c","tag-flipped-mountain-program-in-java","tag-flipped-mountain-program-in-python","tag-flipping-bits-in-c","tag-flipping-bits-in-java","tag-flipping-bits-program-in-c","tag-flipping-bits-solution","tag-flipping-bits-solution-in-c","tag-genpact-interview-questions-and-answers","tag-globallogic-india-pvt-ltd-interview-questions-and-answers","tag-how-to-count-number-of-1s-in-binary-representation-in-c","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-reverse-bytes-in-c","tag-samsung-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-sapient-consulting-pvt-ltd-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-toggle-all-bits-in-c","tag-tracxn-technologies-pvt-ltd-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-wipro-infotech-interview-questions-and-answers","tag-wm-global-technology-services-india-pvt-ltd-limited-wmgts-interview-questions-and-answers","tag-xoriant-solutions-pvt-ltd-interview-questions-and-answers","tag-yodlee-infotech-pvt-ltd-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Count number of bits to be flipped to convert A to B ?<\/title>\n<meta name=\"description\" content=\"How to Count number of bits to be flipped to convert A to B ? - Write the program to count number of bits needed to be flipped to convert \u2018a\u2019 to \u2018b\u2019.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Count number of bits to be flipped to convert A to B ?\" \/>\n<meta property=\"og:description\" content=\"How to Count number of bits to be flipped to convert A to B ? - Write the program to count number of bits needed to be flipped to convert \u2018a\u2019 to \u2018b\u2019.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T17:48:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-13T06:17:20+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/\",\"name\":\"How to Count number of bits to be flipped to convert A to B ?\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-13T17:48:41+00:00\",\"dateModified\":\"2021-09-13T06:17:20+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How to Count number of bits to be flipped to convert A to B ? - Write the program to count number of bits needed to be flipped to convert \u2018a\u2019 to \u2018b\u2019.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Count number of bits to be flipped to convert A to B ?","description":"How to Count number of bits to be flipped to convert A to B ? - Write the program to count number of bits needed to be flipped to convert \u2018a\u2019 to \u2018b\u2019.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/","og_locale":"en_US","og_type":"article","og_title":"How to Count number of bits to be flipped to convert A to B ?","og_description":"How to Count number of bits to be flipped to convert A to B ? - Write the program to count number of bits needed to be flipped to convert \u2018a\u2019 to \u2018b\u2019.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T17:48:41+00:00","article_modified_time":"2021-09-13T06:17:20+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/","name":"How to Count number of bits to be flipped to convert A to B ?","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-13T17:48:41+00:00","dateModified":"2021-09-13T06:17:20+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How to Count number of bits to be flipped to convert A to B ? - Write the program to count number of bits needed to be flipped to convert \u2018a\u2019 to \u2018b\u2019.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-count-number-of-bits-to-be-flipped-to-convert-a-to-b\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":4,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":3488,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/513\/revisions\/3488"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}