bitwise-operation
bitwise-operation 教學與筆記。
題目 1
1 | unsigned long v1 = 0x00001111; |
過程 :
1
2
3
4
5
6
7
8
9
10
11
12v1 0001000100010001
v2 0001001000000010
v1 0001000100010001
~v2 1110110111111101
v1&(~v2) 0000000100010001
v2 0001001000000010
v | v2 0001001100010011
1 3 1 3
Final v = 0x1313實際驗證 :
1
2
3
4
5
6
7
8
9
10
void main(){
unsigned long v1 = 0x00001111;
unsigned long v2 = 0x00001202;
unsigned long v;
v = v1&(~v2);
v = v | v2;
printf("0x%lx\n", v); // 0x1313
}
題目 2
Setting a bit (Force nth bit to 1)
1
2
3
4
5
6
7
void main(){
int a = 76; // 1001100, DEC 76
a |= (1UL << 4);
printf("%d\n", a); // 1011100, DEC 92
}Clearing a bit (Force nth bit to 0)
1
2
3
4
5
6
7
void main(){
int a = 76; // 1001100, DEC 76
a &= ~(1UL << 3);
printf("%d\n", a); // 1000100, DEC 68
}Toggling a bit (Change nth bit “from 1 to 0” or “from 0 to 1”)
1
2
3
4
5
6
7
void main(){
int a = 76; // 1001100, DEC 76
a ^= (1UL << 3);
printf("%d\n", a); // 1011100, DEC 92
}
bitwise-operation