Funny trick in MySQL
A few days ago I needed to get max unsigned big int in MySQL. Here is a little trick on how to get it right away:
mysql> SELECT ~0;
+----------------------+
| ~0 |
+----------------------+
| 18446744073709551615 |
+----------------------+
1 row in set (0.01 sec)
So, what is ~
? It calls bitwise inversion.
But what if I want max signed big int? Here you are:
mysql> SELECT ~0 >> 1;
+---------------------+
| ~0 >> 1 |
+---------------------+
| 9223372036854775807 |
+---------------------+
1 row in set (0.00 sec)
Ok... Let's try something else :)
Ok, I want to have a max unsigned int and signed. It should be >> 32
for unsigned and >> 33
for the signed one. Let's try
mysql> SELECT ~0 >> 32 as 'unsigned',
-> ~0 >> 33 as 'signed';
+------------+------------+
| unsigned | signed |
+------------+------------+
| 4294967295 | 2147483647 |
+------------+------------+
1 row in set (0.01 sec)
Hope someone will find it useful :)