I don’t understand what the bit operations like & and |

5 & 3 gives 1 , and 5 | 3 5 | 3 gives 7 . I thought that it chooses the smallest \ greatest respectively and takes away \ adds difference. But 8 & 5 outputs 0 , not 3 , according to my guesses; 3 | 6 3 | 6 prints 7 , not 9 .

Please help me figure it out. Read on the Internet - difficult and confusing. If the question about the bit is NOT ~ I got the answer that

The bitwise operation NOT for the number x corresponds to -(x+1) so that ~5 gives -6 .

and here everything is immediately clear, then there is no simple answer to bit AND and OR OR .

    2 answers 2

    For the difference, there is a subtraction operation :) It would not hurt you to first understand what bits are. Read a little about the binary system.

    If in a nutshell on your questions. 5 is represented as 101 and 3 as 011 (here we are talking about the representation of decimal numbers in binary numbering system) bitwise и leaves the unit only when both values ​​in the corresponding digit are units

     101 & 011 ___ 001 

    As you can see here, the unit remains where two units in two compared numbers (represented in binary numbering system)

    Consider the second example too. | - bitwise или . "Leaves" units when any of the corresponding digits of numbers is one. Those.

     101 | 011 ___ 111 

    And 111 is 7 in the decimal system.

    That's all.

    Here's a straightforward quote for you to read about bit operations https://server.179.ru/tasks/python/2014b1/22-bits.html - the first link in Google. Here it is also adequately described https://learn.javascript.ru/bitwise-operators

    Well, yes, why can it be useful to you? Well, this is a completely different question :)

      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)