HOWTO · Java

在 Java 中將十進位制轉換為二進位制

本教程演示如何在 Java 中將十進位制轉換為二進位制。

本頁內容

在 Java 中將十進位制轉換為二進位制有不同的方法。本教程演示瞭如何在 Java 中將十進位制轉換為二進位制的不同方法。

我們可以使用 Java 中的按位運算子、陣列和 Math.pow() 方法將十進位制數轉換為二進位制數。

在 Java 中使用位運算子將十進位制轉換為二進位制

位運算子通常比算術運算子工作得更快。它們還可以用於在 Java 中將十進位制轉換為二進位制。參見示例:

package delftstack;

import java.util.Scanner;

class Delftstack {
  public void DecimalToBinary(int number) {
    // Size of the integer is 32 bits
    for (int x = number - 1; x >= 0; x--) {
      int k = number >> x;
      if ((k & 1) > 0) {
        System.out.print("1");
      } else {
        System.out.print("0");
      }
    }
  }
}

public class Decimal_to_Binary {
  public static void main(String[] args) {
    Delftstack Demo = new Delftstack();
    System.out.println("Please enter a number");
    Scanner New_Number = new Scanner(System.in);
    int number = New_Number.nextInt();
    System.out.println("The Input Decimal Number : " + number);
    System.out.print("The Output Binary Number : ");
    Demo.DecimalToBinary(number);
  }
}

上面的程式碼將輸入一個十進位制數字並將該數字轉換為二進位制。檢視輸出:

Please enter a number
50
The Input Decimal Number : 50
The Output Binary Number : 00000000000011001000000000000000000000000000110010

在 Java 中使用陣列將十進位制轉換為二進位制

按照以下步驟使用 Java 中的陣列將十進位制數轉換為二進位制數。

  • 首先,將數字除以 2 時的餘數儲存在陣列中。
  • 接下來,將數字除以二。
  • 然後,我們將重複上述步驟,直到數字大於 0。
  • 最後,以相反的順序列印陣列。

讓我們根據上述步驟嘗試一個示例。

package delftstack;
import java.io.*;
import java.util.Scanner;

class Decimal_to_Binary {
  static void DecimalToBinary(int number) {
    // an array which will store the binary number
    int[] Binary_Number = new int[1000];

    int x = 0;
    while (number > 0) {
      // store the remainder in the array
      Binary_Number[x] = number % 2;
      number = number / 2;
      x++;
    }

    // print the final array in reverse order
    for (int y = x - 1; y >= 0; y--) System.out.print(Binary_Number[y]);
  }

  public static void main(String[] args) {
    System.out.println("Please enter a number");
    Scanner New_Number = new Scanner(System.in);
    int number = New_Number.nextInt();
    System.out.println("The Input Decimal Number :" + number);
    System.out.print("The Output Binary Number : ");
    DecimalToBinary(number);
  }
}

上面的程式碼將輸入一個十進位制數字並將該數字轉換為二進位制。見輸出:

Please enter a number
33
The Input Decimal Number :33
The Output Binary Number : 100001

在 Java 中使用 Math.pow() 將十進位制轉換為二進位制

Math.pow() 方法還可以將十進位制數轉換為二進位制數。讓我們嘗試一個例子。

package delftstack;
import java.io.*;
import java.util.Scanner;

public class Decimal_to_Binary {
  static int DecimalToBinary(int number) {
    int Binary_number = 0;
    int count = 0;
    while (number != 0) {
      int remainder = number % 2;
      double temp = Math.pow(10, count);
      Binary_number += remainder * temp;
      number /= 2;

      count++;
    }

    return Binary_number;
  }

  public static void main(String[] args) {
    System.out.println("Please enter a number");
    Scanner New_Number = new Scanner(System.in);
    int number = New_Number.nextInt();
    System.out.println("The Input Decimal Number : " + number);
    System.out.print("The Output Binary Number : ");
    System.out.println(DecimalToBinary(number));
  }
}

上面的程式碼將輸入一個十進位制數字並將該數字轉換為二進位制。見輸出:

Please enter a number
65
The Input Decimal Number : 65
The Output Binary Number : 1000001