Study Group/빡공단 30기
[Day 10] Operator 3: Bitwise Shift Operator
Fllbert
2023. 1. 11. 00:24
In this class, I learn the bitwise shift operator including '<<' and '>>'.
1. Right-shift operator (>>)
- The right-shift operator (<<) moves the bits of an integer or enumeration type expression to the right.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A = 0b1000;
A = A >> 2;
printf('%x', A);
return 0;
}
The results showed 'A' as 0b0010 that which resulted in change of 2^(-2).
2. Left-shift operator (<<)
- The left-shift operator (<<) moves the bits to the left.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int A = 0b0001;
A = A << 2;
printf('%x', A);
return 0;
}
The results showed 'A' as 0b0100 that which changed by the square of 2.