본문 바로가기
백엔드/Java

자바 정수범위

by david100gom 2024. 3. 20.
// - 2^7 ~ 2^7-1;
byte a = Byte.MAX_VALUE;
byte b = Byte.MAX_VALUE;
 
// - 2^15 ~ 2^15-1
short s1 = Short.MIN_VALUE;
short s2 = Short.MAX_VALUE;
  
// - 2^31 ~ 2^31-1
int i1 = Integer.MIN_VALUE;
int i2 = Integer.MAX_VALUE;
  
// - 2^63 ~ 2^63-1
long h1 = Long.MIN_VALUE;
long h2 = Long.MAX_VALUE;
  
byte aa = (byte) (new BigInteger("2").pow(7).longValue()); //-128 - as expected
short bb = (short) (new BigInteger("2").pow(15).longValue()); //-32768 - as expected
int cc = (int) new BigInteger("2").pow(31).longValue(); //-2147483648 - as expected
long dd = new BigInteger("2").pow(63).longValue(); //-9223372036854775808 - as expected
 
System.out.println(aa);
System.out.println(bb);
System.out.println(cc);
System.out.println(dd);
 
aa = (byte) (new BigInteger("2").pow(7).longValue() - 1); //127 - as expected
bb = (short) (new BigInteger("2").pow(15).longValue() - 1); //32767 - as expected
cc = (int) (new BigInteger("2").pow(31).longValue() - 1); //2147483647 - as expected
dd = (new BigInteger("2").pow(63).longValue() - 1); //9223372036854775807 - as expected
 
System.out.println(aa);
System.out.println(bb);
System.out.println(cc);
System.out.println(dd);

'백엔드 > Java' 카테고리의 다른 글

AES-256  (0) 2024.03.20
SHA-256  (0) 2024.03.20
알고리즘  (0) 2024.03.20
같은 단어 제거 정규식  (0) 2024.03.20
StringBuffer 와 StringBuilder 의 차이  (0) 2024.03.20
ICMP ECHO  (0) 2024.03.20
자바 변수  (0) 2024.03.20
자바 자료구조  (0) 2024.03.20

댓글