Hello!
Explain, please, what are the bitwise operators and what is the principle of their work? I have reread the chapter of K & R several times and read it online, but I don’t understand them.
If possible, with practical examples.
Thank you for understanding.

  • 3
    Yes, of course, now you’ll be sure to tell you from the hashcode better than what is written in K&R - Costantino Rupert
  • Not bitwise, but bitwise. For the rest I agree with the cat. - skegg
  • I really did not understand, I only know and understand >> and <<, and the rest is for me some kind of incomprehensible dark forest. - VladislavMSK
  • one
    For what? C is intended to replace low-level languages ​​(primarily assembly languages). And accordingly it contains a lot of low-level operations, including bit operations on integers (which include symbols). Another unusual feature for high-level language properties is to encourage the use of side effects (and the availability of tools for this). As the saying goes, “C is a macro assembler who considers himself a high-level language” - alexlz
  • one
    As the name suggests, they change / check one or more bits in the machine representation of whole binary (long long, long, int, short, char) numbers. For example, for int x; if (x & 1) // odd - avp

3 answers 3

The principle of operation is extremely simple: we are working with bits of integers. There is, for example, the number 10, it will be 1010 in binary, so if it is int (4 bytes, 32 bits), then it will be:

 0000 0000 . 0000 0000 . 0000 0000 . 0000 1010 

There is, for example, the number 7. It will be equal to:

 0000 0000 . 0000 0000 . 0000 0000 . 0000 0111 

You can produce a conjunction of 7 & 10, i.e. put these numbers on top of each other and carry out the conjunction of each bit of one number with the corresponding bit of another number. Will be:

 0000 0000 . 0000 0000 . 0000 0000 . 0000 0010 

The same logic with "or", i.e. '|' and with "addition modulo 2", i.e. "^".

In the internet a lot of information, of course. You can read more about the shift (bitwise shift).

Used in combinatorics, there are many examples, for example, in the algorithm for generating the set of all subsets (using both "shift" and "bitwise and").

Or maybe it will also be interesting to understand: binary algorithm for finding the GCD of two numbers

  • 2
    How can starting from bits, without affecting the topics of bytes and storing numbers in the computer's memory, go to int? How can the word "conjunction" be used with the "&" sign without explaining their equivalence and identifying them with the operation "and" or "logical and / logical multiplication"? How can you write "the same logic" when the logic is somewhat opposite in the operations you cited? How is it possible to mention all this, forgetting the truth tables, the properties of logical operations, and so on? Are you sure that your explanation will not shock the immature mind? It will not be a confirmation of the words @ Kotik_hohet_kusat? - Dex
  • Truth tables, conjunction, disjunction - this is a 100% school program, I am sure that the author of the question knows all this. Just in his comments, the person wrote: "I only know and understand what a shift is," so I’m sure about int, I bet. PS If it were not for notes on the network and forums, such as hashcode (well, the university is also true), then I would not be able to do anything at all. I didn’t see what was written about this in K & R, but I couldn’t figure out how to use programming books as the main source of information. - ivkremer 4:03 pm
  • one
    Lucky you with the school. I had it in my first year at the institute. And I meant more about your style of presentation, you use terms that are not related to each other in the context of your answer, and therefore may not be clear. UPD Also, I don’t understand why you operate with such small numbers if you take a whole int . UPD2 After reading the commentary, it becomes clear that not quite, apparently, a person is familiar with truth tables. Since it is shifts that somehow relate to them a little, the rest of the operations are based on them. - Dex
  • In this case, you are lucky with the university - we did not have it) by the way, this is included in the EGE on computer science, so ... Many applicants will have to face this before the university is likely to do more confidently =) int for example took from the ceiling . The extra few zeros to write, and then copy-paste is not difficult) Well, yes, they do not apply. And in the school this is not, by the way) they refer to the specified operations, therefore, they are mentioned. - ivkremer 4:47 pm
  • In short, I wrote a practical example (the author asked), said why it is necessary, then I wait for questions in case of misunderstanding. What a good forum of this kind - no one will ever be shy to ask. Thanks for the comments! - ivkremer 4:47 pm

As the name suggests, they change / check one or more bits in the machine representation of whole binary (long long, long, int, short, char) numbers.

For example, for int x;

 if (x & 1) // нечетное х = (х+7) & ~7; // сделаем его ближайшим большим кратным 8 

Etc.

To understand these operations, it is necessary to understand the representation of numbers in machine memory in the bit representation. After that, you yourself will figure out how and when to apply them.

It is quite common to use them for manipulating bitmaps, where individual bits are tightly packed into an array of bytes (or words). For example, an array for 8,000,000 bits will occupy 1,000,000 bytes of memory.

    Bit operations .