BieremaBoyzProgramming / bbpPairings

A Swiss-system chess tournament pairing engine, implementing the Burstein and Dutch systems.
Other
75 stars 30 forks source link

weird logic in code. #10

Closed B-Esmaili closed 4 years ago

B-Esmaili commented 4 years ago

In this operator you've defined a bool value carry which default is false and you make decisions based on it and in else part you assign a uintmax to it.i am really confused about this part.could you please clarify it?

jbierema commented 4 years ago

The variable probably should be called borrow.

Assigning a value of an integer type to a boolean will set the boolean to false if the number is zero and true otherwise.

The unary minus operator behaves the same as subtracting the number from zero. The number is stored in pieces with the least significant piece first. For simplicity, let's act as if each piece is just 4 bits.

If the number (written in hex) is 12A0, then the function is computing 0000 - 12A0. Looking at the least significant pieces first, we subtract the zeroes, and the function will set the least significant piece of the result to 0. For the next pieces from the right, when we try to subtract A from 0, we have to borrow from the next place, so we set carry to true. The resulting piece is 10 - A = 6. Then for the higher pieces, because of the borrow, we are no longer subtracting from 0 but instead subtracting from F. So F - 2 is the bitwise inverse of 2, and F - 1 is the bitwise inverse of 1. These are D and E, respectively. So the result is ED60. If we add this to 12A0, we should get 10000 (in hex).

B-Esmaili commented 4 years ago

@jbierema Thanks for clarification.