본문 바로가기
csapp

2.2.5 Signed versus Unsigned in C

by 정구지개발자 2023. 4. 4.
728x90
  • Generally, most numbers are signed by default. For example, when declaring a constant such as 12345 or 0x1A2B, the value is considered signed. Adding character ‘U’ or ‘u’ as a suffix creates an unsigned constant; for example, 12345U or 0x1A2Bu.
  • When printing numeric values with printf, the directives %d, %u, and %x are used to print a number as a signed decimal, an unsigned decimal, and in hexadecimal format, respectively. Note that printf does not make use of any type information, and so it is possible to print a value of type int with directive %u and a value of type unsigned with directive %d

  • when converting from short to unsigned, the program first changes the size and then the type.

  • That is, (unsigned) sx is equivalent to (unsigned) (int) sx, evaluating to 4,294,954,951, not (unsigned) (unsigned short) sx, which evaluates to 53,191. Indeed, this convention is required by the C standards.

2.2.7 Truncating Numbers

  • rather than extending a value with extra bits, we reduce the number of bits representing a number.
728x90

'csapp' 카테고리의 다른 글

3 Machine-Level Representation of Programs  (0) 2023.04.06
2.2.8 Advice on Signed versus Unsigned  (0) 2023.04.05
2.2.2 Unsigned Encodings  (0) 2023.04.02
2.1.7 Bit-Level Operations in C  (0) 2023.03.31
2.1.3 Addressing and byte Ordering  (0) 2023.03.30

댓글