programing

부호있는 정수와 부호없는 정수의 차이점은 무엇입니까?

nasanasas 2020. 11. 11. 20:13
반응형

부호있는 정수와 부호없는 정수의 차이점은 무엇입니까?


부호있는 정수와 부호없는 정수의 차이점은 무엇입니까?


아시다시피 ints는 내부적으로 바이너리에 저장됩니다. 일반적으로 int에는 32 비트가 포함되지만 일부 환경에서는 16 비트 또는 64 비트 (또는 다른 숫자, 일반적으로 2의 거듭 제곱 일 필요는 없음)를 포함 할 수 있습니다.

그러나이 예에서는 4 비트 정수를 살펴 보겠습니다. 작지만 설명 목적으로 유용합니다.

이러한 정수에는 4 비트가 있으므로 16 개의 값 중 하나를 가정 할 수 있습니다. 16은 2의 4 제곱 또는 2 x 2 x 2 x 2입니다.이 값은 무엇입니까? 답은이 정수가 있는지 여부에 따라 signed int또는를 unsigned int. 를 사용하면 unsigned int값은 절대로 음수가 아닙니다. 값과 관련된 부호가 없습니다. 다음은 4 비트의 16 가지 가능한 값입니다 unsigned int.

bits  value
0000    0
0001    1
0010    2
0011    3
0100    4
0101    5
0110    6
0111    7
1000    8
1001    9
1010   10
1011   11
1100   12
1101   13
1110   14
1111   15

... 그리고 다음은 4 비트의 16 가지 가능한 값입니다 signed int.

bits  value
0000    0
0001    1
0010    2
0011    3
0100    4
0101    5
0110    6
0111    7
1000   -8
1001   -7
1010   -6
1011   -5
1100   -4
1101   -3
1110   -2
1111   -1

보시다시피 signed ints의 1경우 최상위 비트는 숫자가 음수 인 경우에만 해당됩니다. 이것이 signed ints의 경우이 비트를 "부호 비트"라고하는 이유 입니다.


int and unsigned int are two distinct integer types. (int can also be referred to as signed int, or just signed; unsigned int can also be referred to as unsigned.)

As the names imply, int is a signed integer type, and unsigned int is an unsigned integer type. That means that int is able to represent negative values, and unsigned int can represent only non-negative values.

The C language imposes some requirements on the ranges of these types. The range of int must be at least -32767 .. +32767, and the range of unsigned int must be at least 0 .. 65535. This implies that both types must be at least 16 bits. They're 32 bits on many systems, or even 64 bits on some. int typically has an extra negative value due to the two's-complement representation used by most modern systems.

Perhaps the most important difference is the behavior of signed vs. unsigned arithmetic. For signed int, overflow has undefined behavior. For unsigned int, there is no overflow; any operation that yields a value outside the range of the type wraps around, so for example UINT_MAX + 1U == 0U.

Any integer type, either signed or unsigned, models a subrange of the infinite set of mathematical integers. As long as you're working with values within the range of a type, everything works. When you approach the lower or upper bound of a type, you encounter a discontinuity, and you can get unexpected results. For signed integer types, the problems occur only for very large negative and positive values, exceeding INT_MIN and INT_MAX. For unsigned integer types, problems occur for very large positive values and at zero. This can be a source of bugs. For example, this is an infinite loop:

for (unsigned int i = 10; i >= 0; i --) [
    printf("%u\n", i);
}

because i is always greater than or equal to zero; that's the nature of unsigned types. (Inside the loop, when i is zero, i-- sets its value to UINT_MAX.)


Sometimes we know in advance that the value stored in a given integer variable will always be positive-when it is being used to only count things, for example. In such a case we can declare the variable to be unsigned, as in, unsigned int num student;. With such a declaration, the range of permissible integer values (for a 32-bit compiler) will shift from the range -2147483648 to +2147483647 to range 0 to 4294967295. Thus, declaring an integer as unsigned almost doubles the size of the largest possible value that it can otherwise hold.


In laymen's terms an unsigned int is an integer that can not be negative and thus has a higher range of positive values that it can assume. A signed int is an integer that can be negative but has a lower positive range in exchange for more negative values it can assume.


In practice, there are two differences:

  1. printing (eg with cout in C++ or printf in C): unsigned integer bit representation is interpreted as a nonnegative integer by print functions.
  2. ordering: the ordering depends on signed or unsigned specification.

this code can identify the integer using ordering criterion:

char a = 0;
a--;
if (0 < a)
    printf("unsigned");
else
    printf("signed");

참고URL : https://stackoverflow.com/questions/5739888/what-is-the-difference-between-signed-and-unsigned-int

반응형