HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

What does SELECT ~0; do?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
doesselectwhat

Problem

I've recently stumbled into an interesting Mysql query:

SELECT ~0;
results:
18446744073709551615

Which is the max value of a bigint unsigned. What does the tilde operator do in this case?

Solution

It selects the logical negation of 0 and returns it in the implicitly converted datatype. Rather than selecting 8 bytes containing all 0's, it selects 8 bytes containing all 1's, returning the maximum possible unsigned value.

Context

StackExchange Database Administrators Q#82476, answer score: 7

Revisions (0)

No revisions yet.