Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java cộng hai chuỗi nhị phân


Trong bài viết này, chúng ta sẽ tìm hiểu cách cộng hai chuỗi nhị phân trong Java. Chuỗi nhị phân là dãy số biểu diễn bằng byte 0 và 1.

Dưới đây là minh họa:

Đầu vào

Giả sử đầu vào:

10101 10001

Đầu ra

Kết quả mong đợi:

100110

Thuật toán

Bước 1 - BẮT ĐẦU
Bước 2 - Tạo đối tượng Scanner mới
Bước 3 - Nhập hai chuỗi nhị phân
Bước 4 - Định nghĩa cờ carry
Bước 5 - Dùng while kiểm tra khác 0
Bước 6 - Nếu không, dùng toán tử % và cờ carry thực hiện cộng bit
Bước 7 - Hiển thị kết quả
Bước 8 - DỪNG

Ví dụ 1: Nhập từ bàn phím

Người dùng nhập dựa trên lời nhắc. Thử trực tiếp trong coding ground.

import java.util.*;
public class AddBinaryNumbers {
    public static void main(String[] args) {
        long binary_input_1, binary_input_2;
        System.out.println("Required packages have been imported");
        Scanner input = new Scanner(System.in);
        System.out.println("A reader object has been defined");
        System.out.print("Enter the first binary number: ");
        binary_input_1 = input.nextLong();
        System.out.print("Enter the second binary number: ");
        binary_input_2 = input.nextLong();
        int i, carry;
        i = 0;
        carry = 0;
        int[] binary_sum = new int[10];
        while (binary_input_1 != 0 || binary_input_2 != 0) {
            binary_sum[i++] = (int) (carry + (binary_input_1 % 10 + binary_input_2 % 10) % 2);
            carry = (int) ((binary_input_1 % 10 + binary_input_2 % 10 + carry) / 2);
            binary_input_1 = binary_input_1 / 10;
            binary_input_2 = binary_input_2 / 10;
        }
        if (carry != 0) {
            binary_sum[i++] = carry;
        }
        --i;
        System.out.print("\nThe sum of the binary numbers is: ");
        while (i >= 0) {
            System.out.print(binary_sum[i--]);
        }
        System.out.print("\n");
    }
}

Đầu ra

Required packages have been imported
A reader object has been defined
The first binary number is 10101
The second binary number is 10001
The sum of the binary is: 100110

Ví dụ 2: Giá trị định nghĩa trước

Hai số nhị phân đã được định nghĩa trước.

public class AddingBinaryNumbers {
    public static void main(String[] args) {
        long binary_input_1, binary_input_2;
        binary_input_1 = 10101;
        binary_input_2 = 10001;
        System.out.print("The first binary number is " + binary_input_1);
        System.out.print("\nThe second binary number is " + binary_input_2);
        int i, carry;
        i = 0;
        carry = 0;
        int[] binary_sum = new int[10];
        while (binary_input_1 != 0 || binary_input_2 != 0) {
            binary_sum[i++] = (int) (carry + (binary_input_1 % 10 + binary_input_2 % 10) % 2);
            carry = (int) ((binary_input_1 % 10 + binary_input_2 % 10 + carry) / 2);
            binary_input_1 = binary_input_1 / 10;
            binary_input_2 = binary_input_2 / 10;
        }
        if (carry != 0) {
            binary_sum[i++] = carry;
        }
        --i;
        System.out.print("\nThe sum of the binary numbers is: ");
        while (i >= 0) {
            System.out.print(binary_sum[i--]);
        }
        System.out.print("\n");
    }
}

Đầu ra

The first binary number is 10101
The second binary number is 10001
The sum of the binary numbers is: 100110