웹/JavaScript

[Javascript] 2진수, 8진수, 16진수, 비트 연산자, bigInt

Judith Hopps 2023. 1. 13. 17:40
반응형

I. 다른 진법들

👉 MDN 문서 보기

2진법 binary

  • 0b 뒤로 숫자 0, 1 를 붙여 표현
[
0b1,
0b10,
0b11,
0b100,
0b101
].forEach(i => console.log(i))

 

console.log(
0b2 // ⚠️ 토큰으로 인식 - 오류
);



8진법 octal

  • 0o 뒤로 숫자 0~7 를 붙여 표현
[
0o7,
0o10,
0o100,
0o1000,
].forEach(i => console.log(i))

 

console.log(
0o8 // ⚠️ 토큰으로 인식 - 오류
);



16진법 hexadecimal

  • 0x 뒤로 숫자 0~9, A~F 를 붙여 표현
[
0x9,
0xA,
0xB,
0xC,
0xd,
0xe,
0xf,
0x10,
0xFFFFFF
].forEach(i => console.log(i))



⭐️ 진법 간 변환

const num = 123456789;
const binStr = num.toString(2);
const octStr = num.toString(8);
const hexStr = num.toString(16);
console.log(binStr, octStr, hexStr);

 

console.log(
parseInt(binStr, 2),
parseInt(octStr, 8),
parseInt(hexStr, 16)
);
  • 이 외에도 2 ~ 36 사이의 진법 사용 가능 - toString과 parseInt의 가용 인자 범위

 

// 💡 상호변환
console.log(
parseInt(hexStr, 16).toString(2),
parseInt(binStr, 2).toString(8),
parseInt(octStr, 8).toString(16)
);




 

II. 비트 연산자들

👉 MDN 문서 보기

let x = 0b1010101010; // 682
let y = 0b1111100000; // 992

 

// 양쪽 모두 1인 자리에 1
const bitAnd = x & y;
console.log(bitAnd);
console.log(
bitAnd.toString(2)
);

 

// 한 쪽이라도 1인 자리에 1
const bitOr = x | y
console.log(bitOr);
console.log(
bitOr.toString(2)
);

 

// 양쪽이 다른 자리에 1
const bitXor = x ^ y;
console.log(bitXor);
console.log(
bitXor.toString(2)
);

 

// 각 비트 반전
console.log(~x);
console.log(
(~x).toString(2)
);

 

console.log(~y);
console.log(
(~y).toString(2)
);



let x = 0b101; // 5
console.log(x.toString(2), x);

 

// 반복 실행해볼 것, 오른쪽 숫자를 늘려 볼 것
x = x << 1;
console.log(x.toString(2), x);

 

// 반복 실행해볼 것, 오른쪽 숫자를 늘려 볼 것
x = x >> 1;
console.log(x.toString(2), x);
 

Number.MAX_SAFE_INTEGER 더 큰 정수를 다루기 위한 자료형


console.log(
Number.MAX_SAFE_INTEGER
);
  • number 타입으로 안정적으로 표현할 수 있는 가장 큰 정수 - 9007199254740991 (2^53 - 1)
for (let i = 0; i < 100; i++) {
console.log(Number.MAX_SAFE_INTEGER + i);
}


아래의 방법들로 생성

const bigInt1 = 9007199254740991n; // 끝에 n을 붙임
const bigInt2 = BigInt(9007199254740991);
const bigInt3 = BigInt('9007199254740991');
const bigInt4 = BigInt(0x1fffffffffffff) // 9007199254740991
 
console.log(
bigInt1 === bigInt2,
bigInt2 === bigInt3,
bigInt3 === bigInt4
);
 
console.log(typeof bigInt1);
 
for (let i = 0; i < 100; i++) {
console.log(bigInt1 + BigInt(i));
}




BigInt의 특징들

일반 number 타입과 산술 (+, -, *, /, %, **) 연산 불가

console.log(
1n + 1
);
 
console.log(
1n + 1n
);
 
// 양쪽 모두 BigInt로 변환하여 계산하는 방법 사용
const calcAsBigInt = (x, y, op) => {
return op(BigInt(x), BigInt(y));
}
console.log(
calcAsBigInt(1n, 1, (x, y) => x + y)
);


비교 연산 가능

console.log(
1n === 1, // 타입은 다름
1n == 1,
1n < 2,
1n >= 0,
2n < 1
);


number 숫자와 섞여 정렬 가능

console.log(
[4n, 7, 6n, 3, 1, 5, 9, 2n, 8n]
.sort((a, b) => a > b ? 1 : -1)
);


불리언으로 변환되는 연산 가능

console.log(
!!(0n),
!!(1n)
);
 
0n ? console.log('참') : console.log('거짓');
1n ? console.log('참') : console.log('거짓');


소수점 아래는 버림

console.log(
5n / 2n
);


Math의 정적 메서드에서 사용 불가

console.log(
Math.max(1n, 2n)
);


number로 변환 - 정확성 유실 주의!

console.log(
Number(1n),
Number(9007199254740993n)
);
 
 
반응형