기본 콘텐츠로 건너뛰기

3월, 2016의 게시물 표시

[C/C++[ shift 연산 음수로 이용하기

x = k >> -e // 이건 뭐 어쩌자는 건지? c++ 소스 중에  오늘 위와 같은 형식의 shift 연산을 보고 결과가 궁금해서 테스트를 해봤다. 결론은 shift 연산자 우측에 음수가 오면 무조건 0 즉 -e 이렇게 쓴것은 e 의 값이 음수라는 이야기 이다. #include "stdafx.h" template<typename T> void test_minus_shift(T value) { //T value = 1; T nShfitLeft = value << 1; T nShfitLeftMinus = value << -1; T nShfitRight = value >> 1; T nShfitRightMinus = value >> -1; } int _tmain(int argc, _TCHAR* argv[]) { test_minus_shift<int>(1); // nShfitLeft=2, nShfitLeftMinus=-21474836, nShfitRight=0, nShfitRightMinus=0 test_minus_shift<int>(8); // nShfitLeft=16, nShfitLeftMinus=0, nShfitRight=4, nShfitRightMinus=0 test_minus_shift<int>(0xfc31); // nShfitLeft=129122, nShfitLeftMinus=-21474836, nShfitRight=32280, nShfitRightMinus=0 test_minus_shift<unsigned>(1); // nShfitLeft=2, nShfitLeftMinus=21474836, nShfitRight=0, nShfitRightMinus=0 test_minus_shift<unsigned>(8); // nShfitLeft=16,

[C/C++] int형, float형 나눗셈/나머지 구하기 연산 결과

// divmod_test.cpp : int형, float형 div/mod 연산 테스트 // #include "stdafx.h" void test_divmod() { int nzero = 0; //int nd = 20 / nzero; // VS2010 runtime error //int nm = 20 % nzero; // VS2010 runtime error float fzero = 0.f; float fk = 20.f / fzero; // VS2010 no error!!! - fk = '1.#INF000' //float fj = 20 % fzero; // VS2010 compile error - float type not support modulation operation. printf("fk(%f)", fk); } int _tmain(int argc, _TCHAR* argv[]) { test_divmod(); return 0; }

[c++], lvalue, rvalue 에 대한 추가적 정리

 Lvalue를 대입 연산자 왼쪽에 있는 값, RValue를 대입 연산자 오른쪽에 있는값으로 알고 있는 사람이  많습니다. C에서는 이 개념이 맞지만 C++ 98/03에서는 약간 다른 개념으로 사용됩니다. 저도 C에서의 개념으로만 알고 있었는데, 컴파일러와 정적 분석 도구로, 오류 원인을 추적하다 보니, 개념을 좀 더 명확하게 할 필요가 있어서, 구글링(?) 하다가 좋은 자료가 있어 공유 합니다. 참고 링크:  http://scor7910.tistory.com/66 이상입니다.