본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 세균 증식 / Java

by unknownomad 2024. 4. 24.

문제

 

풀이

class Solution {
    public int solution(int n, int t) {
        return n * (int) Math.pow((double) 2, (double) t);
    }
}
class Solution {
    public int solution(int n, int t) {
        for(int i = 0; i < t; i++) {
            n *= 2;
        }
        return n;
    }
}
class Solution {
    public int solution(int n, int t) {
    	// 비트 연산자를 사용하여 정수 n을 t번 왼쪽으로 시프트(shift)한 후 결과를 반환
        int answer = n << t;
        return answer;
    }
}

비트 연산자

  • 여기서 n << t는 n을 t번 왼쪽으로 시프트한 결과를 나타냄
  • 시프트 연산은 이진수의 비트를 왼쪽 또는 오른쪽으로 이동시키는 연산
  • 왼쪽 시프트 연산(<<)은 각 비트를 지정된 횟수만큼 왼쪽으로 이동시킴
  • 비어 있는 비트는 0으로 채워짐
  • 왼쪽 시프트 연산은 원래 숫자를 2의 거듭제곱 만큼 곱하는 효과

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/120910

댓글