It is quite simple.
Bitwise "AND" is an operation on two bits, which gives a unit at the output, when And that, And its other arguments are equal to one: 1 & 1 = 1, 1 & 0 = 0, 0 & 1 = 0, 0 & 0 = 0
Bitwise "OR" is an operation on two bits, which gives a unit at the output, when that OR its other argument is equal to one. If both are the same: 1 & 1 = 1, 1 & 0 = 1, 0 & 1 = 1, 0 & 0 = 0.
And now look at how the “And” bitwise is executed, for example, for numbers 5 and 8. First, we write them in binary form (we have enough one byte for each):
00000101
and
00001000
and now we will carry out the operation "And" as described above, bitwise over the corresponding bits and we will write the result:
00000000, since only those bits are cocked up in the unit, which are set to one in EACH of the arguments.
The result of the operation "OR" will be 00001101, since the bits set to one in ANY of the arguments are cocked into one.
The XOR operation, by the way, is similar to "OR", but cokes the bits in which the operands DIFFER.
Oh yes. The standard application of bit "AND" and "OR" - check / reset and set the bit fields, respectively. I will write in the notation, understand?
unsigned char bitfield = 0x0a; // decimal 10, binary 00001010 ... bitfield = bitfield | 0x01;// устанавливаем самый правый бит в единицу if (bitfield & 0x02) { // проверяем, взведён ли второй справа бит. ... // внутренняя логика этой операции проста: в скобках получится не-ноль // (то есть "истина" в понимании Си) только если в переменной bitfield } // *тоже* взведён второй бит (остальные на результат не влияют, // т.к. при битовом "И" с 00000010 всё равно обращаются в ноль) bitfield = bitfield & 0xfe;// сбрасываем самый правый бит в ноль, не трогая // остальные (0xfe - это 11111110)