• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

바이트와 관련하여서 질문이 있습니다

22.07.05 11:13 작성 조회수 127

0

1비트에는 2가지가 들어갈 수 있고 8비트는 1바이트로 2^8 = 256가지가 들어갈 수 있다는 것을 찾아보았는데요 그렇다면 short의 경우에는 최대 바이트가 2바이트이며 가짓수가 -32767 부터 0 32767까지 이잖아요 근데 여기에서 제가 2바이트를 계산해본 결과 256*256 = 65536이고 이를 2로 나누면 32768인데 그렇다면 디버그해서 나온 값인 32767의 값은 0을 제외하고 계산하여서 이 값이 나온건가요? 어떻게 해서 32767이 나오게 된 건가요? (unsigned을 했을 경우 66535입니다)

답변 1

답변을 작성해보세요.

0

화면가득님의 프로필

화면가득

2023.11.09

Short의 범위는 -32768 ~ 32767입니다.
32768 / 0 / 32767 -> 65536

#include <iostream>
#include <climits>

using namespace std;

int main()
{
	short n_short_min = SHRT_MIN;
	short n_short_max = SHRT_MAX;
	unsigned short n_short_unsigned = -1;

	int n_int_min = INT_MIN;
	int n_int_max = INT_MAX;
	unsigned int n_int_unsigned = -1;

	long n_long_min = LONG_MIN;
	long n_long_max = LONG_MAX;
	unsigned long n_long_unsigned = -1;

	long long n_llong_min = LLONG_MIN;
	long long n_llong_max = LLONG_MAX;
	unsigned long long n_llong_unsigned = -1;

	cout << "short는 " << sizeof n_short_max << " 바이트이다." << endl;
	cout << "이 바이트의 값 범위는 " << n_short_min << " ~ " << n_short_max <<" 이다" << endl;
	cout << "unsigned short는 " << n_short_unsigned << " 이다." << endl << endl;

	cout << "int는 " << sizeof n_int_max << " 바이트이다." << endl;
	cout << "이 바이트의 값 범위는 " << n_int_min << " ~ " << n_int_max <<" 이다" << endl;
	cout << "unsigned int는 " << n_int_unsigned << " 이다." << endl << endl;
		
	cout << "long은 " << sizeof n_long_max << " 바이트이다." << endl;
	cout << "이 바이트의 값 범위는 " << n_long_min << " ~ " << n_long_max << " 이다" << endl;
	cout << "unsigned long은 " << n_long_unsigned << " 이다." << endl << endl;
	
	cout << "long long은 " << sizeof n_llong_max << " 바이트이다." << endl;
	cout << "이 바이트의 값 범위는 " << n_llong_min << " ~ " << n_llong_max << " 이다" << endl;
	cout << "unsigned long long은 " << n_llong_unsigned << " 이다." << endl << endl;

	return 0;
}