# Algorithm

This week’s LeetCode problem is 371. Sum of Two Integers

Given two integers a and b, return the sum of the two integers without using the operators + and -.

`Input: a = 1, b = 2Output: 3`

The four cases according to the addition of two binary bits are as follows:

`0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 0 (carry)`

In the case of disregarding the carry, the result of the addition without **carry is ** `a^b`; and whether or not to carry depends on `a&b`, so the result of the carry is `(a&b)<<1`. Thus, we can split the sum of integers a and b into the sum of the carry-free addition of a and b and the carry-over result. Because each split can shift the least significant bit to the left by at least one bit, and because a and b can take negative numbers. Because signed integers are represented in two's complement, the above method also works for 0 and negative numbers.

`class Solution {    public int getSum(int a, int b) {        while (b != 0) {            int carry = (a & b) << 1;            a = a ^ b;            b = carry;        }        return a;    }}`

# Review

This week’s Review is for the following article: Best practices for writing code comments

A lot of code comments does not mean its quality is guaranteed. Good comments should help others to understand the code more easily. Here are some suggestions from the author on how to ensure the quality of the code:

Rule 1: Comments should not duplicate the code.

`i = i + 1; // Add one to i`

A more extreme example.

`// create a for loop // <-- commentfor // start for loop( // round bracket     // newlineint // type for declarationi // name for declaration= // assignment operator for declaration0 // start value for i`

Rule 2: Good comments do not excuse unclear code.

Bad example, meaning of variable `n` is ambiguous

`private static Node getBestChildNode(Node node) {     Node n; // best child node candidate     for (Node node: node.getChildren()) {         // update n if the current state is better         if (n == null || utility(node) > utility(n)) {             n = node;         }     }     return n;}`

For a good example, the variable `n` should be renamed to `bestNode`, so that it can be understood by others without the need for comments.

`private static Node getBestChildNode(Node node) {     Node bestNode;     for (Node currentNode: node.getChildren()) {         if (bestNode == null || utility(currentNode) > utility(bestNode)) {             bestNode = currentNode;         }     }     return bestNode;}`

Rule 3: If you can’t write a clear comment, there may be a problem with the code. The most infamous comment in the Unix source code is “You are not expected to understand this,” which appeared before some hairy context-switching code. Unfortunately, it turned out that he and co-author Ken Thompson didn’t understand it themselves and later had to rewrite it.

Rule 4: Comments should dispel confusion, not cause it. If your comment causes confusion instead of dispelling it, remove it.

Rule 5: Explain unidiomatic code in comments.

Bad example, code should not explain code that everyone can understand, unless you are writing a tutorial for newbies.

`final Object value = (new JSONTokener(jsonString)).nextValue();// Note that JSONTokener.nextValue() may return// a value equals() to null.if (value == null || value.equals(null)) {     return null;}`

Rule 6: Provide links to the original source of copied code. Here is a good example:

`/** Converts a Drawable to Bitmap. via https://stackoverflow.com/a/46018816/2219998. */return (int) (0.3 * red + 0.59 * green + 0.11 * blue);`

Rule 7: Include links to external references where they will be most helpful. Sometimes, a link to the manual can also be given:

`// http://tools.ietf.org/html/rfc4180 suggests that CSV lines// should be terminated by CRLF, hence the \r\n.csvStringBuilder.append("\r\n");`

`// NOTE: At least in Firefox 2, if the user drags outside of the browser window,// mouse-move (and even mouse-down) events will not be received until// the user drags back inside the window. A workaround for this issue// exists in the implementation for onMouseLeave().@Overridepublic void onMouseMove(Widget sender, int x, int y) { .. }`

Rule 9: Use comments to mark incomplete implementations. Here is a example:

`// TODO(hal): We are making the decimal separator be a period, // regardless of the locale of the phone. We need to think about // how to allow comma as decimal separator, which will require // updating number parsing and other places that transform numbers // to strings, such as FormatAsDecimal`

In fact, many code comments are actually the same as shown in the following image:

# Tip

Similarities and differences between the `clock()` function and the `gettimeofday()` function:

• Common point: both can obtain the current time, and then obtain the running time of a certain code by calculating the difference between the current time before and after
• Difference: `clock()` is a C language library function, which means it can be used under any system; while the `gettimeofday()` function is only a function in Linux system, so it can only be used in Linux system use.

# Share

The trilogy of tutorials on reading and writing CSV in C language has been released. The reading data of each platform is obviously more than that of the ARTS series. It can be seen that the platform and everyone prefer tutorials with a specific point, and more articles of this type will be created in the future.

--

--