따배씨++ (2.2 integer)

김동우·2021년 3월 3일
0

안녕하십니까. 김동우입니다.

이번 글에서는 정수형과 다양한 함수들을 사용해보겠습니다.

#include <iostream>

int main() 
{
	using namespace std;

	// signed int의 경우
	// byte의 가장 첫 bit를 부호에 사용한다.
	// 00000000 or 10000000

	int			i(1);   // 4 bytes = 4 * 8bit = 32bits, 2^32
	short		s(1);	// 4 bytes = 2 * 8bit = 16bits, 2^16
	long		l(1);	// 4 bytes = 4 * 8bit = 32bits, 2^32
	long long	ll(1);	// 4 bytes = 8 * 8bit = 64bits, 2^64

	// init

	cout << sizeof(int) << endl;				//output : 4
	cout << sizeof(short) << endl;				//output : 2
	cout << sizeof(long) << endl;				//output : 4
	cout << sizeof(long long) << '\n' << endl;	//output : 8

	cout << pow(2, sizeof(short) * 8 - 1) - 1 << '\n' << endl;

	// sizeof(short) * 8 - 1 : 1을 뺀 이유는 맨 앞 비트는 음과 양 결정
	// 괄호 밖에서 -1을 진행한 것은 0을 빼준 것으로,
	// 해당 수식은 양의 정수 범위를 나타낸다.

	cout << numeric_limits<short>::max() << endl;	//output : 32767
	cout << numeric_limits<short>::min() << endl;	//output : -32768
	cout << numeric_limits<short>::lowest() << 
		'\n' << endl;								//output : -32768

	// 해당 함수는 수의 범위 최대, 최소값을 출력하는 것으로 생각하자.
	// min(), lowest()의 경우 integer 형에서는 차이가 없다.
	// 포인트는 이 범위를 갖는 수를 사용할 때, 범위를 벗어나지 않아야 한다.

	s = numeric_limits<short>::max(); // init : s = 1, now : 32767
	s = s + 1; // 32767 + 1;

	cout << s << '\n' << endl; //output : -32768 

	// 위처럼 범위를 벗어난 경우 overflow가 발생한다.
	// overflow가 발생하면 이진수 연산에서 1을 추가로 더했기 때문에
	// 가장 작은 수인 -32768이 된 것이다.
	// 1이 아닌 수를 더해보면 가장 작은 수인 -32768에서부터 덧셈
	// 15를 넣을 경우 -32768 + (15 - 1) = -32754가 된다.

	s = numeric_limits<short>::min(); // init : s = 1, now : 32767
	s = s - 1; // -32768 - 1;

	cout << s << '\n' << endl; //output : 32767

	// 이를 통해 overflow가 발생할 경우 값이 반전되는 것을 볼 수 있다.
	// 2진수 연산의 심오함(?)

// -------------------------------------------------------------------

	unsigned int a = -1; 

	cout << a << '\n' << endl; //output : 4294967295

	// unsigned형에 음수를 집어넣을 경우, 위처럼 overflow가 발생한다.

// -------------------------------------------------------------------

	int d(22 / 4);

	cout << d << endl;						//output : 5

	// 5.5가 나와야 하는데, 값이 5로 잘린 것을 볼 수 있다.

	cout << (float)d << endl;				//output : 5

	cout << (float)22 / 4 << endl;			//output : 5.5
	cout << 22 / (float)4 << '\n' << endl;	//output : 5.5

	// 위처럼 수식의 일부가 float인 경우, float의 형태로 출력한다.
	// 그러나 int로 선언한 d를 float으로 출력할 수는 없다.

	return 0;
}

자세한 노트가 궁금하시다면, 코드를 복사해서 cpp 파일로 한 번 읽어보시는 것을 추천드립니다.

numeric_limits, sizeof 등과 같은 함수는 std에서 나온 함수입니다.

  1. fixed-width integer
#include <iostream>
// #include <cstdint> // iostream을 include 할 경우에는 주석

int main()
{
	using namespace std;

	// 고정너비정수 (fixed-width integers)

	int16_t i(5);
	int8_t myint(65);

	cout << myint << endl; // output : A

	// int8_t의 경우 signed char 선언과 동일하다.
	// typedef를 확인하면 알 수 있다.

	int_fast8_t fi(5);
	int_least64_t fl(5); // 8byte dtype = long long 으로 def

	cout << fi << endl; // output : 

	// 가장 빠른 data형인데 signed char로 def되어 있다.

	return 0;
}

2번째로 적은 글은 고정너비정수에 대한 글입니다. 해당 내용은 당장은 필요 없지만, 후에 사용될 수 있는 개념이기 때문에 추가했습니다.

자, 그럼 이번 글은 여기서 마치도록 하겠습니다.

0개의 댓글

관련 채용 정보