~/devlog/bitwise-cpp

Bitwise Operations and Bitfields in C++

2016/07/06 23:56

Basics of Logic:

0 && 0 == 0
0 && 1 == 0
1 && 1 == 1
0 || 0 == 0
0 || 1 == 1
1 || 1 == 1
!0 == 1
!1 == 0

Bitwise operations:

1 & 0 == 0
1 | 0 == 1

unsigned char data = 0;
unsigned char flag = 4;
unsigned char flag2 = 8;
data = 00000000
flag = 00000100
flag2 = 00001000

data = data | flag;
data = 00000100

data = data | flag2;
data = 00001100

00001100
&
00000100
--------
00000100 // now we know that flag is set into data

data = ~data;
data = 11110011

.code .c++