In-reply-to » Eehhh, what the hell is going on here!?

@lyse@lyse.isobeef.org

Disclaimer: Can’t guarantee that I’m fully awake and I’m being trained at work not to use my brain anymore, so maybe this is complete bullshit. 😪🧟‍♀️

It says here that SQLite uses signed integers:

https://sqlite.org/datatype3.html

In pure bits, 1 << 63 would be 0x8000000000000000, but as a signed value, it gets interpreted as -9223372036854775808. Subtracting 1 yields -9223372036854775809 – but that doesn’t fit in 64 bits anymore. It’s possible that SQLite doesn’t want to wrap around but instead saturates? Haven’t checked. 🤔

With 62 bits, there is enough room.

With 1 << 64, I have no idea how SQLite wants to handle this, because this should immediately trigger a warning, because it doesn’t fit right away. Maybe it gets truncated to 0?

sqlite> select printf('0x%x', 2 * (1 << 64));
╭──────────────────────╮
│ printf('0x%x', 2 ... │
╞══════════════════════╡
│ 0x0                  │
╰──────────────────────╯
sqlite> select printf('0x%x', 0 - 1);
╭──────────────────────╮
│ printf('0x%x', 0 ... │
╞══════════════════════╡
│ 0xffffffffffffffff   │
╰──────────────────────╯
sqlite> select printf('0x%x', 0 - 2);
╭──────────────────────╮
│ printf('0x%x', 0 ... │
╞══════════════════════╡
│ 0xfffffffffffffffe   │
╰──────────────────────╯

⤋ Read More