int 보다 작은 데이터 형은 연산전에 int (정수) 형으로 캐스팅 된다.
uint8_t port = 0x5a;uint8_t temp = (~port);uint8_t result_8 = temp >> 4;
|
Noncompliant Code Example
This noncompliant code example demonstrates how performing bitwise operations on integer types smaller than int may have unexpected results.
uint8_t port = 0x5a;uint8_t result_8 = ( ~port ) >> 4; |
In this example, a bitwise complement of port is first computed and then shifted 4 bits to the right. If both of these operations are performed on an 8-bit unsigned integer, then result_8 will have the value 0x0a. However, port is first promoted to a signed int, with the following results (on a typical architecture where type int is 32 bits wide):
Expression | Type | Value | Notes |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
| Whether or not value is negative is implementation-defined. |
|
|
|
|
Compliant Solution
In this compliant solution, the bitwise complement of port is converted back to 8 bits. Consequently, result_8 is assigned the expected value of 0x0aU.
uint8_t port = 0x5a;uint8_t result_8 = (uint8_t) (~port) >> 4; |
'학습자료(~2017) > 팁' 카테고리의 다른 글
| IT 뉴스 사이트 (0) | 2013.03.19 |
|---|---|
| [리눅스]쉘 명령어 중첩하기 (0) | 2013.01.23 |
| [리눅스] rpm 패키지 파일의 의존 리스트(목록) 확인 (0) | 2012.12.13 |
| ca-bundle.crt, ca bundle file 이란, howto create CA bundle file, CA bundle 파일 만들기 (2) | 2012.11.23 |
| 프로그램 개발시 사용되는 것들 자료조사 (0) | 2012.08.29 |