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

 
Các thành phần theming views field templates in Drupal 7

Các thành phần theming views field templates in Drupal 7

Today I needed to work the Nivo featured content slider into a D7 site I’m building; no problem you might say, there’s a module for that! Sadly that’s not the case as it’s far from ready (there is now a working D7 plugin for Nivo slider please see the links for details).

Adding a WYSIWYG Editor to Your Drupal Site

Thêm WYSIWYG Editor trong Drupal Site của bạn

WYSIWYG stands for "What You See Is What You Get" ... if you select some text and click the "bold" button, that text will be bold. If you select some text and click the "italic" button, that text will be in italics. There's no messing around with HTML. That will be a relief to some of you and more particularly ... your clients and site visitors.

63 hướng dẫn best practice to optimize PHP code performances

63 hướng dẫn best practice to optimize PHP code performances

Today i am searching solutions for any tips and tricks about best practice to optimize PHP code performances and i found some of useful articles which you may interested with.

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

 

Diet con trung