这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.baeldung.arbitrarylengthbinaryintegers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;

public class BigIntegerExample {

private static final Logger log = LoggerFactory.getLogger(BigIntegerExample.class);

public static void main(String[] args) {
BigInteger a = new BigInteger("1101001011010101010101010101010101010101010101010101010101010101010", 2);
BigInteger b = new BigInteger("-10000", 2);

log.info(getBinaryOperations(a, b));

BigInteger c = new BigInteger("11111111111111111111111111111000", 2);
log.info("c = " + c.toString(2) + " (" + c + ")");
}

public static String getBinaryOperations(BigInteger a, BigInteger b) {
return "a = " + a.toString(2) + " (" + a + ")\n" +
"b = " + b.toString(2) + " (" + b + ")\n" +
"a + b = " + a.add(b).toString(2) + " (" + a.add(b) + ")\n" +
"a - b = " + a.subtract(b).toString(2) + " (" + a.subtract(b) + ")\n" +
"a * b = " + a.multiply(b).toString(2) + " (" + a.multiply(b) + ")\n" +
"a / b = " + a.divide(b).toString(2) + " (" + a.divide(b) + ")";
}

public static BigInteger add(BigInteger a, BigInteger b) {
return a.add(b);
}

public static BigInteger subtract(BigInteger a, BigInteger b) {
return a.subtract(b);
}

public static BigInteger multiply(BigInteger a, BigInteger b) {
return a.multiply(b);
}

public static BigInteger divide(BigInteger a, BigInteger b) {
return a.divide(b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.baeldung.arbitrarylengthbinaryintegers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BinaryLiterals {

private static final Logger log = LoggerFactory.getLogger(BinaryLiterals.class);

public static void main(String[] args) {
int a = 0b110100101101010;
int b = 0b1000;

log.info(getBinaryOperations(a, b));

int c = 0b11111111111111111111111111111000; // (2^32 - 8) written in base 2
log.info("c = " + Integer.toBinaryString(c) + " (" + c + ")");
}

public static String getBinaryOperations(int a, int b) {
return "a = " + Integer.toBinaryString(a) + " (" + a + ")\n" +
"b = " + Integer.toBinaryString(b) + " (" + b + ")\n" +
"a + b = " + Integer.toBinaryString(a + b) + " (" + (a + b) + ")\n" +
"a - b = " + Integer.toBinaryString(a - b) + " (" + (a - b) + ")\n" +
"a * b = " + Integer.toBinaryString(a * b) + " (" + (a * b) + ")\n" +
"a / b = " + Integer.toBinaryString(a / b) + " (" + (a / b) + ")";
}

public static int add(int a, int b) {
return a + b;
}

public static int subtract(int a, int b) {
return a - b;
}

public static int multiply(int a, int b) {
return a * b;
}

public static int divide(int a, int b) {
return a / b;
}
}
Loading