BigInteger
정수의 크기에 제한 없이 정밀한 정수 계산을 위한 자바의 클래스.
문제 풀때 int쓰면 갑자기 음수뜨거나 0뜨거나 진짜 이해할 수 없는 결과가 뜰 때, 이거 써야함
import java.math.BigInteger;
이거 임포트 먼저 해줘야 쓸 수 있다.
너무 귀찮다. 파이썬할까
1. BigInteger 생성자
- BigInteger(String val) : 객체를 생성
- BigInteger(String val, int radix) : radix; 진법
**여기서부터 쓰고 싶으면
n.divide(m)
이딴꼴로 써주셔야 함
2. 산술 연산 함수
- add(BigInteger val) : 더하기
- subtract(BigInteger val) : 빼기
- multiply(BigInteger val) : 곱하기
- divide(BigInteger val) : 몫
- remainder(BigInteger val) : 나머지
3. 비트 연산 함수
- and(BigInteger val): and
- or(BigInteger val): or
- xor(BigInteger val): xor
- shiftLeft(int n): shiftleft
- shiftRight(int n): shiftright
4. 비교 함수
- compareTo(BigInteger val) :
- n.compareTo(m) 기준, n < m이면 음수, n = m 이면 0, n > m 이면 양
- equals(Object obj) : T/F로 반환
- max(BigInteger val): 큰놈 반환
- min(BigInteger val) : 작은놈
**BigInteger bi = num.intValue()
5. 변환 함수:
- intValue(): int로
- longValue(): long로
- toString(): 문자열로
- int to BigInteger
//#1. 문자에 담긴 값 바꾸고 싶어
int intValue = 12345;
BigInteger bigInteger = new BigInteger(String.valueOf(intValue));
//#2. int 2 BigInteger
BigInteger binInteger2 = new BigInteger("1");
728x90
'JAVA 당' 카테고리의 다른 글
[eclipse 에러 해결] java.lang.UnsupportedClassVersionError: * has been compiled by a more recent version of the Java Runtime (0) | 2025.03.12 |
---|---|
Spring boot에서 테스트코드 만들고 실행하는 법 (w. intellij) (1) | 2024.01.27 |
openCV 를 위한 데이터 셋 구성과 spring boot, MySQL연동하기 (0) | 2023.11.23 |
그래프정리 및 구현 (0) | 2023.07.24 |
[JAVA] 열받는 자바문법 : Scanner, BufferedReader (0) | 2023.06.21 |