목록Programming Language (46)
배움 저장소

이 글에서는 left shift와 right shift 연산이 어떤 차이가 있는지 다룬다. 이러한 차이가 코드에서 예기치 못한 상황을 만들 수 있다. left shift와 right shift 연산의 차이를 알아보자. 1. signed && 음수 signed값은 맨 앞 bit가 양수 / 음수를 나타낸다. 위 값은 음수이므로 >> 연산자를 사용할 때 부호 값은 그대로 유지된다. 2. unsigned || signed && 양수 위 상황은 unsgined 타입이거나 signed 타입일 경우 양수 값을 가질 때이다. 이 때 >> 연산자를 사용하면 부호 값은 계산되지 않는다. 따라서 다음과 같은 상황을 이해할 수 있다. char x = CHAR_MAX; printf("%i\n", x); // => 127 cha..

4.1 문자열 입출력하기 char fruit; char fruit2[40]; /* char type 40개 저장공간을 확보 */ scanf("%c", &fruit); scanf("%s", fruit2); /* 이때 fruit2는 char type 40개 저장공간의 첫 번째를 가리키는 포인트다. 따라서 & 연산자를 사용할 필요가 없다.*/ printf("%c", fruit); printf("%s", fruit2); // 사용공간을 지정해주지 않고 초기화 가능하다 // 자리 계산은 알아서 해준다 char fruit3[] = "banana"; 4.2 sizeof 연산자 1) sizeof basic types int i = 0; unsigned int int_size1 = sizeof i; unsigned in..
3.10 문자형 #include int main() { char c = 'A'; char d = 65; printf("%c %hhd\n", c,c); // -> A 65 printf("%c %hhd\n", d,d); // -> A 65 printf("%c %hhd\n", ',d); // -> A 65 printf("%c %hhd\n", d,d); // -> A 65 printf("\a"); // -> beef alarm printf("\07"); // -> 8진수 7 ( character 'a'와 동일) printf("\x7"); // -> 16진수 7 float salary; printf("$______\b\b\b\b\b\b");// $____ scanf("%f", &salary); // ^ 해당 위치..
1. unique_ptr 여러 포인터가 하나의 대상을 가르키는 걸 방지해준다. 해당 값을 가리키는 ptr를 한 개로 제한하고 싶을때 사용하면 된다. 만약 여러 unique_ptr이 하나의 대상을 가르키면 컴파일 에러가 난다. 특정 데이터의 소유권을 다른 unique_ptr로 옮기려면 move()함수를 이용하자 unique_ptr ptr1 (new A); // Error: can't copy unique_ptr unique_ptr ptr2 = ptr1; // Works, resource now stored in ptr2 unique_ptr ptr2 = move(ptr1); 예제 // C++ program to illustrate the use of unique_ptr #include #include usi..

Why is 1000 = -8 in signed 4-bit binary? I was wondering why 1000 in signed 4-bit binary notation is equal to -8 in decimal. From my understanding, the 1 simply states that sign (1 being negative and 0 being positive) it has no real decimal value. I have read that it is not possible to correctly www.physicsforums.com Signed int의 첫 번째 비트는 부호를 나타내고 나머지 비트는 숫자를 나타낸다. Signed Int의 양수 4비트로 숫자를 표현해보자 양..