SQLite User Forum

Support Binary representation like 0b01
Login

Feature Request: Support Binary representation like 0b01

(1.2) By Denis TRUFFAUT (DenisTRUFFAUT) on 2023-04-01 14:28:08 edited from 1.1 [source]

SQLite supports Hexadecimal representation, like 0x01:

select 0x01, 0x10
1 16

But SQLite doesn't seem to support Binary representation, like 0b01:

select 0b01, 0b10
unrecognized token: "0b01"

Should be:

1 2

Binary representation exists in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

Example:

console.log(0b01, 0b10)
1 2

Binary representation is useful for intersecting bitmaps, bitsets, bitstrings : https://roaringbitmap.org/

This helps to express multiple values inside a single field, which avoid joins, which improves performance.

For example if you wonder how two records intersect

0b111001110001110
0b001001011001000

In JavaScript:

0b111001110001110 & 0b001001011001000
4744

(0b111001110001110 & 0b001001011001000).toString(2)
'1001010001000'

In SQLite (When this feature will be available):

select 0b111001110001110 & 0b001001011001000
4744

Therefore, please support binary representation, like 0b01.

(2) By Donal Fellows (dkfellows) on 2023-04-03 15:19:50 in reply to 1.2 [link] [source]

Binary representation exists in JavaScript

And many other languages besides (such as Python and Tcl).

Which isn't to make an argument for or against in this case.

(4) By jose isaias cabrera (jicman) on 2023-04-03 15:31:34 in reply to 2 [link] [source]

Which isn't to make an argument for or against in this case.

You may not have wanted to make an argument, but, by saying so, you did. And for it. :-)

(3) By anonymous on 2023-04-03 15:26:29 in reply to 1.2 [link] [source]

This seems like a good candidate for a binary() user defined function.

(5) By Keith Medcalf (kmedcalf) on 2023-04-03 15:39:52 in reply to 1.2 [link] [source]

Actually no, SQLite does not support any representation other than base-10.

You may use base-16 input tokens and they will (mayhaps) be interpreted correctly, bu you may not use base-16 representation of values (well, actually you can, but they are just a bag-o-bytes (that is, 0xffff is a text string bag of bytes, not a number)).

Are you suggesting that you wold want base-2 input tokens (not values). What about base-8 input tokens (not values).

Or are you somehow under the misunderstanding that SQLite recognized base-16 values rather than merely base-16 tokens?

(6) By anonymous on 2023-04-03 16:09:45 in reply to 5 [link] [source]

0xffff is a text string bag of bytes, not a number

This is the SQLite forum, not the MySQL forum.

sqlite> select typeof(0xffff);
+----------------+
| typeof(0xffff) |
+----------------+
| integer        |
+----------------+

(8) By Keith Medcalf (kmedcalf) on 2023-04-03 16:54:43 in reply to 6 [link] [source]

Exactly. SQlite3 only allows use of hexidecimal notation in tokens, not values.

SQLite version 3.42.0 2023-04-03 04:59:10
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> create table x(x integer);
sqlite> insert into x values (0xffff);
sqlite> select x, typeof(x) from x;
┌───────┬───────────┐
│   x   │ typeof(x) │
├───────┼───────────┤
│ 65535 │ 'integer' │
└───────┴───────────┘

You will note that the value of x is the integer 65535, not the token-string 0xffff. You can convert the integer 65535 to the character-token-string 0xffff, but then it is a bag-o-bytes text string, not an integer.

sqlite> select y, typeof(y) from (select format('0x%x', x) as y from x);
┌──────────┬───────────┐
│    y     │ typeof(y) │
├──────────┼───────────┤
│ '0xffff' │ 'text'    │
└──────────┴───────────┘

(7) By Larry Brasfield (larrybr) on 2023-04-03 16:46:37 in reply to 5 [link] [source]

SQLite does not support any representation other than base-10. ...

The docs say hexadecimal literals are accepted.

(9) By Keith Medcalf (kmedcalf) on 2023-04-03 16:55:35 in reply to 7 [link] [source]

Yes, not hexideciaml values.

(10) By anonymous on 2023-04-03 17:23:04 in reply to 5 [link] [source]

Actually no, SQLite does not support any representation other than base-10.

Actually, SQLite integers are represented with sixty-four digits using base two.

(11) By Larry Brasfield (larrybr) on 2023-04-03 17:36:18 in reply to 10 [link] [source]

Actually, SQLite integers are represented with sixty-four digits using base two.

Actually, the cell representation is often less than 64 bits.

At a lower level, the representation is often higher than or lower than nominal charge in a few femptoFarads of capacitance, or relative positions of minute region magnetic flux reversals.

(12) By brickviking on 2023-04-04 10:18:58 in reply to 11 [link] [source]

Would that take into account allowances for quantum-affected variations?

REg4rd5, br!ckv1King

(Post 10)

(13) By Larry Brasfield (larrybr) on 2023-04-04 18:02:19 in reply to 12 [link] [source]

Would that take into account allowances for quantum-affected variations?

Not quite, as stated. I just find it amusing to see "actually" associated with the imaginary "bits". We wish they were truly binary, but reality intervenes with finite Signal-to-Noise-Ratio, quantum effects and uncertainties, and occasional cosmic rays.

(14) By Donal Fellows (dkfellows) on 2023-04-05 08:43:03 in reply to 5 [link] [source]

SQLite does not support any representation other than base-10.

As I read it, this request isn't about the representation in storage, but the textual representation as a literal value in an SQL script. That is, it is an SQL parser matter and not something that the bytecode engine (or below) need to care about at all.

(15) By Denis TRUFFAUT (DenisTRUFFAUT) on 2023-04-08 14:41:34 in reply to 14 [link] [source]

Yes, exactly.