Bit-Level Operations in C
Operations &, |, ~, ^ available in C
• Apply to any "integral" data type - long, int, short, char, unsigned
Q) 나옴
~0x00 -> 0xFF
Logic Operations in C
T/F?
• &&, ||, !
• View 0 as "False"
• Anything nonzero as "True"
• Always return 0 or 1
Early termination // 앞부터 비교하기 때문에 적게 비교하는 게 더 좋음
Q) 나옴
char형
!0x41 | 0x00 |
!0x00 | 0x01 |
!!0x41 | 0x01 |
0x69 && 0x55 | 0x01 |
0x69 || 0x55 | 0x01 |
if (p && *p) // avoids null pointer access
Shift Operations
Left shift: x << y
• Shift bit-vector x left y positions
- Throw away extra bits on left - Fill with 0's on right
Right shift: x >> y
• Shift bit-vector x right y positions
- Throw away extra bits on right
• Logical shift
- Fill with 0's on left
• Arithmetic shift
- Replicate MSB on right - Useful with two's complement integer representation
Undefined if y < 0 or y ≥ word size
char형에 -128~127 사이의 정수를 나타낼 때도 사용할 수 있음!
void main() {
char i = -2; // char은 8bit임!! 1111 1110
printf("%d\n", i);
i = i >> 2; // 1111 1111(2)
printf("%d\n", i); // -1
}
"Arithmetic shift right" is the default operation in C
2 << 2 ? 8
오버플로우는 2의 보수
'전공 > 시스템프로그래밍' 카테고리의 다른 글
[09] ASM2 : Control Flow (1) | 2022.10.28 |
---|---|
[04] Floating Point (0) | 2022.10.28 |
[05] BYTE ORDERING (0) | 2022.10.27 |
[08] ASM1 (0) | 2022.10.25 |
[07] Introduction to IA-32 (0) | 2022.10.09 |