One of the most popular and perhaps most spread way to represent negative integers in computing is called Two's complement.
Two's complement is a mathematical operation on binary numbers, as well as a binary signed number representation based on this operation
By its definition, to convert a given integer into a negative integer we have to flip the bits by applying the NOT operation and add 1. Two's complement can handle a range expressed by the following formula:
−(2^N−1) to +(2^N−1 − 1)
Our 3 bit example system would therefore cover values from -4 to +3. Its binary representation is shown in the following table:
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
I'm convinced things start to get sense to you now. In the binary table above we can clearly see that negative 1 integer will be represented as 111. As we also know the ~ symbol which is a bitwise NOT operation that inverts the bits the question now is:
So by the following code:
Such idioms are sometimes hard to read but once fully understood it becomes usually a shorter way to write a code. Some programmers don't like these constructs as they are harder to read. I'm not going to agree or defend these opinions as in the end they are question of preferences. The thing is that It's important to be able to read them because you never know where you might need them.
Of course, the whole idea about insertion sort being done in T-SQL is absurd. The following lines were written just for fun and also to point out that it happens surprisingly often that I see other programmers approaching the SQL in a procedural way. SQL is a declarative language and works the best when is approached in a declarative way. It's not C# or object oriented language in general. Rather a set oriented language.
How often have you seen usage of cursors, loops and other similar constructs in T-SQL on cases where it could have been done just using a plain SQL? It wasn't so rare in my career. Such an usage is sometimes necessary however many times it was a quick and of course a dirty solution for a given task.
If you don't know what the insertion sort is check the wikipedia page about insertion sort
Consider the insertion sort algorithm that is in O(N^2). The following T-SQL code tries to sort an array of integers by implementing a couple of LOOP statements.
First, let's prepare example data
This simply adds 1000 of random integers to an array. No big deal. Now, let's implement the insertion sort.
T-SQL implementation of insertion sort
Terrible, right? I know. When you could simply do the following instead:
Here comes the best part. Compare the times of both methods. On my Core i7, 8GB RAM machine the results are as follows:
This is of course an extreme case. I believe nobody would really consider the sorting being done manually. This article only demonstrates that T-SQL implementations quite often defeat the holy idea of SQL.
Blog about my programming experiments, tests and so on.