# Convert the given BCD to its equivalent Binary form

Given a BCD (Binary Coded Decimal), the task is to convert this to its equivalent Binary number.

Examples:

Input: 1001000
Output: 110000
Explanation:
Integer value of the given BCD is 48(0100 -> 4, 1000 -> 8).
(48)10 = (110000)2

Input: 1001001
Output: 110001

Approach: In order to solve this problem we need to split the given BCD number into binary chunks of length 4 and convert them to integer one by one to generate the final integer representation of the given BCD. Once generated, convert the integer to its binary form.

Below is the implementation of the above approach.

## C++

 // C++ code to convert BCD to its // equivalent Binary    #include using namespace std;    // Function to convert BCD to Decimal string bcdToBinary(string s) {     int l = s.length();     int num = 0;     int mul = 1;     int sum = 0;     // If the length of given BCD is not     // divisible by 4     for (int i = l % 4 - 1; i >= 0; i--) {         sum += (s[i] - '0') * mul;         mul *= 2;     }        num = sum;        sum = 0;     mul = pow(2, 3);     int ctr = 0;     for (int i = l % 4; i < l; i++) {         ctr++;         sum += (s[i] - '0') * mul;         mul /= 2;         if (ctr == 4) {             num = num * 10 + sum;             sum = 0;             mul = pow(2, 3);             ctr = 0;         }     }        // Convert decimal to binary     string ans = "";     while (num > 0) {         ans += (char)(num % 2 + '0');         num /= 2;     }        reverse(ans.begin(), ans.end());        return ans; }    // Driver Code int main() {     string s = "1001000";        // Function Call     cout << bcdToBinary(s);        return 0; }

Output:

110000

Time Complexity: O(N) where N denotes the length of the BCD string provided
Auxiliary Space: O(1)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.