An accident caused by an AND operation

There was an accident today. The investigation continued and a piece of old code was found.

The code logic is very simple:

<?php
$a = 1;
$b = 8;
if ($a & $b) {
    
    
    var_dump('same');
} else {
    
    
    var_dump('not same');
}

What is the purpose of this code? Determine whether a and b are equal.

Why can it be judged? In other words, under what conditions does this logic take effect?

Smart you may have the answer, when both a and b are 2^n.

In the bitwise AND operation, only the same a&b can be greater than zero. Otherwise, each bit must have a 0, and the final result is 0, which means it is not equal.

This is a relatively clever way of writing (there are few people writing this way), and in theory, this operation is faster than directly judging equality.

But it hides two huge problems

  1. The value will increase very quickly, quickly reaching the range of crossing the boundary, after all, it will increase exponentially
  2. If this logic has always been maintained by the same person, it’s okay, knowing that the assignment must be 2^n. If the code is handed over to another group or a new person is changed, it is very likely that you don’t know this, and the index rule is not followed when assigning. It's a tragedy

Murphy's law is a question of probability. I feel that nothing in the world can escape statistics, and then it really is a tragedy. For the system, how much an & is worth, may be worthless, or it may be a sky-high price.

Later I thought about it, there are at least two problems at the code level

  1. If you take over someone else’s code, you must read it again. In addition, you must follow up on the places that are not in line with common sense. These places are often the hiding places of the law.
  2. When writing code, easy-to-understand is the most important thing. Don't show off your skills, and don't bury pits just to show off your skills. As far as the code in the example above is concerned, that is, every number is not fully utilized, and errors are likely to occur. In addition, the performance cannot be improved much. Optimizing the logic and changing the algorithm will not improve the performance much than this?

One final word:

<?php
$a = 1;
$b = 8;
if ($a == $b) {
    
    
    var_dump('same');
} else {
    
    
    var_dump('not same');
}

Isn't this way of writing fragrant?

At last

If you like my article, you can follow my official account (Programmer Mala Tang)

My personal blog is: https://shidawuhen.github.io/

Review of previous articles:

technology

  1. Spike system
  2. Distributed system and consensus protocol
  3. Service framework and registry of microservices
  4. Beego framework usage
  5. Talking about microservices
  6. TCP performance optimization
  7. Current limit realization 1
  8. Redis implements distributed locks
  9. Golang source code bug tracking
  10. The realization principle of transaction atomicity, consistency and durability
  11. Detailed CDN request process
  12. Common caching techniques
  13. How to efficiently connect with third-party payment
  14. Gin framework concise version
  15. A brief analysis of InnoDB locks and transactions
  16. Algorithm summary

study notes

  1. Agile revolution
  2. How to exercise your memory
  3. Simple logic-after reading
  4. Hot air-after reading
  5. The Analects-Thoughts after Reading
  6. Sun Tzu's Art of War-Thoughts after reading

Thinking

  1. Project process management
  2. Some views on project management
  3. Some thoughts on product managers
  4. Thoughts on the career development of programmers
  5. Thinking about code review
  6. Markdown editor recommendation-typora

Guess you like

Origin blog.csdn.net/shida219/article/details/110252433