Core Java

Converting BCD to Decimal in Java

Binary-Coded Decimal (BCD) is a system of representing decimal numbers where each digit (0–9) is encoded using four binary bits. Unlike pure binary representation, BCD allows a clearer mapping between human-readable digits and their binary equivalents, making it useful in calculators, digital clocks, and embedded systems. Let us delve into understanding BCD to Decimal conversion in Java.

1. What is BCD?

Binary-Coded Decimal (BCD) is a method of representing each decimal digit using its binary equivalent. In this system, each decimal digit from 0 to 9 is represented using a 4-bit binary code. For instance, the decimal number 59 is split into two digits — 5 and 9 — and each digit is encoded separately as 0101 and 1001 respectively.

1.1 What is BCD Format?

Each decimal digit is stored in a 4-bit binary format. Here’s how individual digits are represented:

  • Decimal 3 → 0011
  • Decimal 5 → 0101
  • Decimal 9 → 1001

Hence, the decimal number 59 in BCD is represented as 0101 1001. Each 4-bit group (nibble) corresponds to a single decimal digit, allowing easy extraction and conversion. BCD is widely used in systems that require exact decimal accuracy — such as calculators, digital clocks, and embedded devices — where rounding errors in floating-point arithmetic are not acceptable.

1.2 BCD and Pure Binary

In pure binary, the entire number is represented as one continuous binary sequence. For example:

  • Decimal 59 → Binary 111011

However, in BCD, each digit is encoded separately as its 4-bit binary equivalent:

  • Decimal 59 → BCD 0101 1001

This format is less memory-efficient since each digit occupies 4 bits even if not needed (e.g., for 0–9), but it makes displaying and converting to human-readable decimal much simpler.

2. Code Example

// Complete Example: BCD to Decimal Conversion in Java
public class BCDToDecimalExample {

  // Method 1: Convert using Bitwise Operations
  public static int bcdToDecimalBitwise(int bcdValue) {
    int decimal = 0;
    int multiplier = 1;

    while (bcdValue > 0) {
      int nibble = bcdValue & 0xF; // extract last 4 bits
      decimal += nibble * multiplier; // add the digit
      multiplier *= 10;
      bcdValue >>= 4; // shift right 4 bits for next digit
    }

    return decimal;
  }

  // Method 2: Convert using Array of Digits
  public static int bcdToDecimalArray(int[] bcdDigits) {
    int decimal = 0;
    for (int digit: bcdDigits) {
      decimal = decimal * 10 + digit; // reconstruct the decimal number
    }
    return decimal;
  }

  public static void main(String[] args) {
    // Example 1: Bitwise Conversion
    int bcdValue = 0x59; // BCD representation of 59 (0101 1001)
    int decimal1 = bcdToDecimalBitwise(bcdValue);

    System.out.println("=== Bitwise Conversion ===");
    System.out.println("BCD (Hex): 0x" + Integer.toHexString(bcdValue));
    System.out.println("Decimal: " + decimal1);

    // Example 2: Array-Based Conversion
    int[] bcdArray = {
      5,
      9
    }; // BCD digits representing 59
    int decimal2 = bcdToDecimalArray(bcdArray);

    System.out.println("\n=== Array-Based Conversion ===");
    System.out.print("BCD Digits: [");
    for (int i = 0; i < bcdArray.length; i++) {
      System.out.print(bcdArray[i]);
      if (i < bcdArray.length - 1) System.out.print(", ");
    }
    System.out.println("]");
    System.out.println("Decimal: " + decimal2);
  }
}

2.1 Code Explanation

The above Java program demonstrates two methods to convert a Binary-Coded Decimal (BCD) value into its decimal equivalent. The first method, bcdToDecimalBitwise(), uses bitwise operations where the value is processed nibble by nibble (4 bits at a time). In each iteration, the last 4 bits are extracted using the bitwise AND operator (& 0xF), multiplied by a position-based multiplier (1, 10, 100, …), and then added to the cumulative decimal result. The input is right-shifted by 4 bits (>>= 4) to process the next BCD digit until all digits are converted. The second method, bcdToDecimalArray(), takes an array of BCD digits and reconstructs the decimal number by repeatedly multiplying the current total by 10 and adding the next digit, effectively joining the digits into one number. In the main() method, a BCD value 0x59 (which represents 59 in BCD) is passed to both methods. The bitwise method decodes it directly from its hexadecimal form, and the array-based method reconstructs it from the array {5, 9}. Both approaches print the same output confirming the correctness of conversion.

=== Bitwise Conversion ===
BCD (Hex): 0x59
Decimal: 59

=== Array-Based Conversion ===
BCD Digits: [5, 9]
Decimal: 59

3. Conclusion

BCD (Binary-Coded Decimal) offers a practical way to store and process decimal digits in binary-based systems. Using bitwise or array-based approaches in Java, we can easily convert BCD values back into decimal form. The bitwise method is efficient for encoded binary values, while the array method is simpler and more readable for digit-level operations.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button