Chuyển từ hệ thập phân sang hệ nhị phân bằng ngôn ngữ Java

Chuyển từ hệ thập phân sang hệ nhị phân bằng ngôn ngữ Java

Có 3 cách để chuyển một số từ hệ thập phân sang hệ nhị phân.

  1. Sử dụng phương thức toBinaryString() trong class Integer của Java.
  2. Sử dụng phương thức chuyển đổi bằng logic mà không sử dụng phương thức có sẵn của Java.
  3. Sử dụng Stack.

1. Sử dụng phương thức toBinaryString().

package simplecodecjava.blogspot.com;


public class DecimalToBinaryExample {

 public static void main(String[]args){

  int decimalNumber = 123456789;

  System.out.print("Hệ nhị phân của " + decimalNumber +" là :");

  System.out.print(Integer.toBinaryString(decimalNumber));

 }

}

Output: Hệ nhị phân của 123456789 là :111010110111100110100010101

2. Sử dụng chuyển đổi bằng logic.

package simplecodecjava.blogspot.com; 

public class DecimalToBinaryExample {  

 public void convertBinary(int num){

      int binary[] = new int[40];

      int index = 0;

      while(num > 0){

        binary[index++] = num%2;

        num = num/2;

      }

      for(int i = index-1;i >= 0;i--){

        System.out.print(binary[i]);

      }

   }
   

 public static void main(String[]args){

  int decimalNumber = 123456789;

  System.out.print("Hệ nhị phân của " + decimalNumber +" là :");

  new DecimalToBinaryExample().convertBinary(decimalNumber);

 }

}

Output: Hệ nhị phân của 123456789 là :111010110111100110100010101

3. Sử dụng Stack

package simplecodecjava.blogspot.com;

import java.util.Stack;

public class DecimalToBinaryExample { 

 public void convertBinary(int num){

  Stack<Integer> stack = new Stack<Integer>();

     while(num != 0){

      //thực hiện phép chia lấy phần dư cho 2.

      int d = num%2;

      // thêm vào stack.

      stack.push(d);

      num/=2;

     }

     while(!(stack.isEmpty())){

      System.out.print(stack.pop());

     }
     

   }
  

 public static void main(String[]args){

  int decimalNumber = 123456789;

  System.out.print("Hệ nhị phân của " + decimalNumber +" là :");

  new DecimalToBinaryExample().convertBinary(decimalNumber);

 }

}

Output: Hệ nhị phân của 123456789 là :111010110111100110100010101

Bạn thấy bài viết này như thế nào?: 
Average: 8 (2 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Gaming Becomes Exciting With 3D Bowling App for Android

Gaming Becomes Exciting With 3D Bowling App for Android

The Android market has so many games for users to choose from. The reality is not all these games are that great.

Google TV 2.0 chạy vi xử lí ARM ra mắt tại CES 2012

Google TV 2.0 chạy vi xử lí ARM ra mắt tại CES 2012

Thế hệ Google TV tiếp theo sẽ sử dụng bộ vi xử lí ARM sau khi nhà sản xuất chip Marvell cho biết...

Ví dụ đơn giản về sử dụng PHPUnit và Drupal 8 năm 2015

Ví dụ đơn giản về sử dụng PHPUnit và Drupal 8 năm 2015

So, instead of duplicating what’s already out there, I’m going to focus on some principles 

Công ty diệt chuột T&C

 

Diet con trung